Вы здесь
Главная > Мой блог > WEB-разработка > Перевод текста в транслит и UTF-8

Перевод текста в транслит и UTF-8

Раньше пользовался функцией:
function GetInTranslit($string) {
$string=strtr($string,”абвгдеёзийклмнопрстуфхцыэАБВГДЕЁЗИЙКЛМНОПРСТУФХЦЫЭ”,

“abvgdeeziyklmnoprstufhcieABVGDEEZIKLMNOPRSTUFHCIE”);
$string=strtr($string, array(
“ж”=>”zh”, ” “=>”-“, “ч”=>”ch”, “ш”=>”sh”,
“щ”=>”shch”, “ь”=>””, “ъ”=>””, “ю”=>”yu”, “я”=>”ya”,
“Ж”=>”Zh”, “Ч”=>”Ch”, “Ш”=>”Sh”,
“Щ”=>”Shch”, “Ь”=>””, “Ъ”=>””, “Ю”=>”Yu”, “Я”=>”Ya”,
“і”=>”i”, “І”=>”I”, “ї”=>”yi”, “Ї”=>”Yi”, “є”=>”ie”, “Є”=>”Ye”)
);
return $string;
}

но, когда текст в UTF-8 выходят крокозяблы. 🙁 Правильный вариант:

function GetInTranslit($string) {
$replace=array(
“‘”=>””,
“`”=>””,
“а”=>”a”,”А”=>”a”,
“б”=>”b”,”Б”=>”b”,
“в”=>”v”,”В”=>”v”,
“г”=>”g”,”Г”=>”g”,
“д”=>”d”,”Д”=>”d”,
“е”=>”e”,”Е”=>”e”,
“ж”=>”zh”,”Ж”=>”zh”,
“з”=>”z”,”З”=>”z”,
“и”=>”i”,”И”=>”i”,
“й”=>”y”,”Й”=>”y”,
“к”=>”k”,”К”=>”k”,
“л”=>”l”,”Л”=>”l”,
“м”=>”m”,”М”=>”m”,
“н”=>”n”,”Н”=>”n”,
“о”=>”o”,”О”=>”o”,
“п”=>”p”,”П”=>”p”,
“р”=>”r”,”Р”=>”r”,
“с”=>”s”,”С”=>”s”,
“т”=>”t”,”Т”=>”t”,
“у”=>”u”,”У”=>”u”,
“ф”=>”f”,”Ф”=>”f”,
“х”=>”h”,”Х”=>”h”,
“ц”=>”c”,”Ц”=>”c”,
“ч”=>”ch”,”Ч”=>”ch”,
“ш”=>”sh”,”Ш”=>”sh”,
“щ”=>”sch”,”Щ”=>”sch”,
“ъ”=>””,”Ъ”=>””,
“ы”=>”y”,”Ы”=>”y”,
“ь”=>””,”Ь”=>””,
“э”=>”e”,”Э”=>”e”,
“ю”=>”yu”,”Ю”=>”yu”,
“я”=>”ya”,”Я”=>”ya”,
“і”=>”i”,”І”=>”i”,
“ї”=>”yi”,”Ї”=>”yi”,
“є”=>”e”,”Є”=>”e”
);
return $str=iconv(“UTF-8″,”UTF-8//IGNORE”,strtr($string,$replace));
}

Добавить комментарий

Top