Skip to content

Instantly share code, notes, and snippets.

@ildarkhasanshin
Forked from dmacompton/translit.php
Last active January 20, 2020 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ildarkhasanshin/fd6aca64ba82843716921cbcdb6dfdc4 to your computer and use it in GitHub Desktop.
Save ildarkhasanshin/fd6aca64ba82843716921cbcdb6dfdc4 to your computer and use it in GitHub Desktop.
Translit php
function translit_str( $str, $maxLength = 100, $encode = 'utf-8' ) {
$tr = array(
"А" => "a",
"Б" => "b",
"В" => "v",
"Г" => "g",
"Д" => "d",
"Е" => "e",
"Ё" => "yo",
"Ж" => "zh",
"З" => "z",
"И" => "i",
"Й" => "y",
"К" => "k",
"Л" => "l",
"М" => "m",
"Н" => "n",
"О" => "o",
"П" => "p",
"Р" => "r",
"С" => "s",
"Т" => "t",
"У" => "u",
"Ф" => "f",
"Х" => "h",
"Ц" => "c",
"Ч" => "ch",
"Ш" => "sh",
"Щ" => "shch",
"Ъ" => "",
"Ы" => "y",
"Ь" => "",
"Э" => "e",
"Ю" => "yu",
"Я" => "ya",
"а" => "a",
"б" => "b",
"в" => "v",
"г" => "g",
"д" => "d",
"е" => "e",
"ё" => "yo",
"ж" => "zh",
"з" => "z",
"и" => "i",
"й" => "y",
"к" => "k",
"л" => "l",
"м" => "m",
"н" => "n",
"о" => "o",
"п" => "p",
"р" => "r",
"с" => "s",
"т" => "t",
"у" => "u",
"ф" => "f",
"х" => "h",
"ц" => "c",
"ч" => "ch",
"ш" => "sh",
"щ" => "shch",
"ъ" => "",
"ы" => "y",
"ь" => "",
"э" => "e",
"ю" => "yu",
"я" => "ya",
" " => "-",
"." => "",
"," => "",
"/" => "",
"\"" => "",
"'" => "",
"/" => "",
":" => "",
"(" => "",
")" => "",
"!" => "",
"&" => "",
""" => "",
"«" => "",
"»" => "",
"%" => "",
"-" => "-",
"&" => "",
"$" => "",
"«" => "",
"»" => "",
"+" => "",
";" => "",
"’" => "",
);
$res = trim( strtr( $str, $tr ), "-" );
$tr2 = array(
'---' => '-',
);
$res = strtr( $res, $tr2 );
$res = mb_strtolower( $res, $encode );
$res = mb_substr( $res, 0, $maxLength, $encode );
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment