Skip to content

Instantly share code, notes, and snippets.

@artemsites
Last active November 16, 2024 15:04
Show Gist options
  • Save artemsites/3dbdd8a84a10dd8d9c37845a35b259ef to your computer and use it in GitHub Desktop.
Save artemsites/3dbdd8a84a10dd8d9c37845a35b259ef to your computer and use it in GitHub Desktop.
<?php
function transliterate($text, $lowercase=false, $spaceToDash=false) {
$transliterationTable = array(
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
'е' => 'e', 'ё' => 'yo', 'ж' => 'zh', 'з' => 'z', 'и' => 'i',
'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n',
'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't',
'у' => 'u', 'ф' => 'f', 'х' => 'kh', 'ц' => 'ts', 'ч' => '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', 'Х' => 'Kh', 'Ц' => 'Ts', 'Ч' => 'Ch',
'Ш' => 'Sh', 'Щ' => 'Shch', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '',
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya',
);
$res = strtr($text, $transliterationTable);
if ($lowercase) {
$res = strtolower($res);
}
if ($spaceToDash) {
$res = str_replace(" ", "-", $res);
}
return $res;
}
// Пример использования
// $originalText = "Привет, как дела?";
// $transliteratedText = transliterate($originalText);
// echo $transliteratedText; // Вывод: Privet, kak dela?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment