Created
May 5, 2020 04:28
-
-
Save gartes/f854e0e0b3f1f75a0a20a21af9847222 to your computer and use it in GitHub Desktop.
PHP Транслитерация
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Транслитерация строк. | |
function transliterate($st) { | |
$st = strtr($st, | |
"абвгдежзийклмнопрстуфыэАБВГДЕЖЗИЙКЛМНОПРСТУФЫЭ", | |
"abvgdegziyklmnoprstufieABVGDEGZIYKLMNOPRSTUFIE" | |
); | |
$st = strtr($st, array( | |
'ё'=>"yo", 'х'=>"h", 'ц'=>"ts", 'ч'=>"ch", 'ш'=>"sh", | |
'щ'=>"shch", 'ъ'=>'', 'ь'=>'', 'ю'=>"yu", 'я'=>"ya", | |
'Ё'=>"Yo", 'Х'=>"H", 'Ц'=>"Ts", 'Ч'=>"Ch", 'Ш'=>"Sh", | |
'Щ'=>"Shch", 'Ъ'=>'', 'Ь'=>'', 'Ю'=>"Yu", 'Я'=>"Ya", | |
)); | |
return $st; | |
} | |
echo transliterate("У попа была собака, он ее любил."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rus2translit($string) { | |
$converter = 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', | |
); | |
return strtr($string, $converter); | |
} | |
function str2url($str) { | |
// переводим в транслит | |
$str = rus2translit($str); | |
// в нижний регистр | |
$str = strtolower($str); | |
// заменям все ненужное нам на "-" | |
$str = preg_replace('~[^-a-z0-9_]+~u', '-', $str); | |
// удаляем начальные и конечные '-' | |
$str = trim($str, "-"); | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment