Skip to content

Commit

Permalink
В модуль Text добавлен метод транслитерации
Browse files Browse the repository at this point in the history
  • Loading branch information
mzhelskiy committed Mar 17, 2015
1 parent 1d050ca commit ae7dfb5
Showing 1 changed file with 95 additions and 1 deletion.
96 changes: 95 additions & 1 deletion classes/modules/text/Text.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ public function CodeSourceParser($sText)
* @param string $sText Исходный текст
* @return array
*/
public function Cut($sText) {
public function Cut($sText)
{
$sTextShort = $sText;
$sTextNew = $sText;
$sTextCut = null;
Expand All @@ -335,4 +336,97 @@ public function Cut($sText) {

return array($sTextShort, $sTextNew, $sTextCut ? htmlspecialchars($sTextCut) : null);
}

/**
* Выполняет транслитерацию текста
*
* @param $sText
* @param bool $bLower
* @return mixed|string
*/
public function Transliteration($sText, $bLower = true)
{
$aConverter = array(
'а' => 'a',
'б' => 'b',
'в' => 'v',
'г' => 'g',
'д' => 'd',
'е' => 'e',
'ё' => 'e',
'ж' => 'zh',
'з' => 'z',
'и' => 'i',
'й' => 'y',
'к' => 'k',
'л' => 'l',
'м' => 'm',
'н' => 'n',
'о' => 'o',
'п' => 'p',
'р' => 'r',
'с' => 's',
'т' => 't',
'у' => 'u',
'ф' => 'f',
'х' => 'h',
'ц' => 'c',
'ч' => 'ch',
'ш' => 'sh',
'щ' => 'sch',
'ь' => "'",
'ы' => 'y',
'ъ' => "'",
'э' => 'e',
'ю' => 'yu',
'я' => 'ya',
'А' => 'A',
'Б' => 'B',
'В' => 'V',
'Г' => 'G',
'Д' => 'D',
'Е' => 'E',
'Ё' => 'E',
'Ж' => 'Zh',
'З' => 'Z',
'И' => 'I',
'Й' => 'Y',
'К' => 'K',
'Л' => 'L',
'М' => 'M',
'Н' => 'N',
'О' => 'O',
'П' => 'P',
'Р' => 'R',
'С' => 'S',
'Т' => 'T',
'У' => 'U',
'Ф' => 'F',
'Х' => 'H',
'Ц' => 'C',
'Ч' => 'Ch',
'Ш' => 'Sh',
'Щ' => 'Sch',
'Ь' => "'",
'Ы' => 'Y',
'Ъ' => "'",
'Э' => 'E',
'Ю' => 'Yu',
'Я' => 'Ya',
" " => "-",
"." => "",
"/" => "-",
"_" => "-"

This comment has been minimized.

Copy link
@psnet

psnet Mar 19, 2015

Туда же украинский алфавит:

'і' => 'i',
'І' => 'I',
'ї' => 'i',
'Ї' => 'I',
'є' => 'e',
'Є' => 'E',
'ґ' => 'g',
'Ґ' => 'G',
/*
 * эти лапки iconv заменяет знаками вопроса
 */
'«' => '',
'»' => '',

не знаю почему паника у iconv на кавычки, но суть такова

This comment has been minimized.

Copy link
@mzhelskiy

mzhelskiy Mar 19, 2015

Author Contributor

тикет

);
$sRes = strtr($sText, $aConverter);
if ($sResIconv = @iconv("UTF-8", "ISO-8859-1//IGNORE//TRANSLIT", $sRes)) {
$sRes = $sResIconv;
}
$sRes = preg_replace('/[^A-Za-z0-9\-]/', '', $sRes);
$sRes = preg_replace('/\-+/', '-', $sRes);

This comment has been minimized.

Copy link
@psnet

psnet Mar 19, 2015

заменить на

$sRes = preg_replace('#\-{2,}#', '-', $sRes);

чтобы не менять один "-" на тот же один "-"

if ($bLower) {
$sRes = strtolower($sRes);
}
return $sRes;
}
}

0 comments on commit ae7dfb5

Please sign in to comment.