Skip to content

Instantly share code, notes, and snippets.

@tot-ra
Last active December 15, 2015 03:09
Show Gist options
  • Save tot-ra/5192149 to your computer and use it in GitHub Desktop.
Save tot-ra/5192149 to your computer and use it in GitHub Desktop.
Cyryllic transliteration to latin
function Translit_iso($str) {
$str=strtr($str,"абвгдезиклмнопрстуфцъыь","abvgdeziklmnoprstufс\"y’");
$str=strtr($str,"АБВГДЕЗИКЛМНОПРСТУФЦЪЫЬ", "ABVGDEZIKLMNOPRSTUFС\"Y’");
$str=strtr($str, array( "э"=>"eh", "х"=>"kh", "й"=>"jj", "ё"=>"jo", "ж"=>"zh", "ч"=>"ch", "ш"=>"sh", "щ"=>"shh", "ю"=>"yu", "я"=>"ya", "э"=>"eh", "х"=>"kh", "й"=>"jj", "ё"=>"jo", "ж"=>"zh", "ч"=>"ch", "ш"=>"sh", "щ"=>"shh", "ю"=>"yu", "я"=>"ya", "ї"=>"i", "ї"=>"yi", "є"=>"ie", "є"=>"ye" ));
return $str;
}
function toTranslit($text, $type = 'translit') {
$data = explode(" ", $text);
if (count($data) == '') {
return '';
}
$alphas = array('ts' => 'ц', 'ch' => 'ч', 'sh' => 'ш', 'shc' => 'щ', 'je' => 'э', 'yii' => 'ы', 'yu' => 'ю', '\'' => 'ь', '\'' => 'ъ', 'ja' => 'я', 'yo' => 'ё', 'a' => 'а', 'b' => 'б', 'v' => 'в', 'g' => 'г', 'd' => 'д', 'e' => 'е', 'j' => 'ж', 'z' => 'з', 'i' => 'и', 'j' => 'й', 'k' => 'к', 'l' => 'л', 'm' => 'м', 'n' => 'н', 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 't' => 'т', 'u' => 'у', 'f' => 'ф', 'h' => 'х');
$total = '';
foreach ($data as $k => $v) {
if (preg_match("/^[a-zA-Z]*/", $v)) {
foreach ($alphas as $id => $value) {
if ($type == 'de') {
if (strcasecmp($v, $id) AND !eregi("->", $v)) {
$v = str_replace($id, $value, $v);
}
elseif (eregi("->", $v)) {
$v = str_replace("->", "", $v);
}
}
elseif ($type = 'translit') {
if (strcasecmp($v, $value) AND !eregi("->", $v)) {
$v = str_replace($value, $id, $v);
}
elseif (eregi("->", $v)) {
$v = str_replace("->", "", $v);
}
}
}
}
$total .= $v . " ";
}
return $total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment