34 votes

Translittération cyrillique en PHP

Comment translittérer les caractères cyrilliques en lettres latines ?

E.g.   -> Glavnaja stranica

Ce site Extension PHP pour la translittération ferait très bien l'affaire, mais je ne peux pas l'installer sur mon serveur.

Il serait préférable d'avoir la même implémentation mais en PHP.

3voto

Galaxy IT Points 121

Si vous voulez une double conversion avec une précision à chaque lettre, vous devez améliorer un peu les méthodes. J'ai la traduction utilisée pour l'url, et le paramètre url est recherché dans la base de données. C'est pourquoi il est très important pour moi de conserver les caractères exacts sans remplacer l'un par l'autre.

! !! Support des symboles ukrainiens.

/**
 * @param $string
 *
 * @return string only cyrillic letter
 */
function to_cyrillic($string):string
{
    $gost = [
        "a" => "а", "b" => "б", "v" => "в", "g" => "г", "d" => "д", "e" => "е", "yo" => "ё",
        "j" => "ж", "z" => "з", "ii" => "и", "ji" => "й", "k" => "к",
        "l" => "л", "m" => "м", "n" => "н", "o" => "о", "p" => "п", "r" => "р", "s" => "с", "t" => "т",
        "y" => "у", "f" => "ф", "h" => "х", "c" => "ц",
        "ch" => "ч", "sh" => "ш", "sch" => "щ", "ie" => "ы", "u" => "у", "ya" => "я", "A" => "А", "B" => "Б",
        "V" => "В", "G" => "Г", "D" => "Д", "E" => "Е", "Yo" => "Ё", "J" => "Ж", "Z" => "З", "I" => "И", "Ji" => "Й",
        "K" => "К", "L" => "Л", "M" => "М",
        "N" => "Н", "O" => "О", "P" => "П",
        "R" => "Р", "S" => "С", "T" => "Т", "Y" => "Ю", "F" => "Ф", "H" => "Х", "C" => "Ц", "Ch" => "Ч", "Sh" => "Ш",
        "Sch" => "Щ", "Ie" => "Ы", "U" => "У", "Ya" => "Я", "'" => "ь", "_'" => "Ь", "''" => "ъ", "_''" => "Ъ",
        "yi" => "ї", "ge" => "ґ",
        "ye" => "є",
        "Yi" => "Ї",
        "II" => "І",
        "Ge" => "Ґ",
        "YE" => "Є",
    ];
    return strtr($string, $gost);
}

/**
 * @param $string
 *
 * @return string only latin letter
 */
function to_latin($string):string
{
    $gost = [
        "а" => "a", "б" => "b", "в" => "v", "г" => "g", "д" => "d",
        "е" => "e", "ё" => "yo", "ж" => "j", "з" => "z", "и" => "ii",
        "й" => "ji", "к" => "k", "л" => "l", "м" => "m", "н" => "n",
        "о" => "o", "п" => "p", "р" => "r", "с" => "s", "т" => "t",
        "у" => "y", "ф" => "f", "х" => "h", "ц" => "c", "ч" => "ch",
        "ш" => "sh", "щ" => "sch", "ы" => "ie", "э" => "e", "ю" => "u",
        "я" => "ya",
        "А" => "A", "Б" => "B", "В" => "V", "Г" => "G", "Д" => "D",
        "Е" => "E", "Ё" => "Yo", "Ж" => "J", "З" => "Z", "И" => "I",
        "Й" => "Ji", "К" => "K", "Л" => "L", "М" => "M", "Н" => "N",
        "О" => "O", "П" => "P", "Р" => "R", "С" => "S", "Т" => "T",
        "У" => "Y", "Ф" => "F", "Х" => "H", "Ц" => "C", "Ч" => "Ch",
        "Ш" => "Sh", "Щ" => "Sch", "Ы" => "Ie", "Э" => "E", "Ю" => "U",
        "Я" => "Ya",
        "ь" => "'", "Ь" => "_'", "ъ" => "''", "Ъ" => "_''",
        "ї" => "yi",
        "і" => "ii",
        "ґ" => "ge",
        "є" => "ye",
        "Ї" => "Yi",
        "І" => "II",
        "Ґ" => "Ge",
        "Є" => "YE",
    ];
    return strtr($string, $gost);
}

1voto

fnatic Points 11

Puisque tous les éléments ci-dessus sont incomplets, voici ma version :

    $textcyr="Тест на кирилице";
    $textlat="Test na kirilitse!";
         $cyr  = array('а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п','р','с','т','у', 
            'ф','х','ц','ч','ш','щ','ъ', 'ы','ь', 'э', 'ю','я',
            'А','Б','В','Г','Д','Е','Ё','Ж','З','И','Й','К','Л','М','Н','О','П','Р','С','Т','У',
            'Ф','Х','Ц','Ч','Ш','Щ','Ъ', 'Ы','Ь', 'Э', 'Ю','Я' );
$lat = array( 'a','b','v','g','d','e','e','zh','z','i','y','k','l','m','n','o','p','r','s','t','u',
            'f' ,'h' ,'ts' ,'ch','sh' ,'sht' ,'i', 'y', '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' ,'Ts' ,'Ch','Sh' ,'Sht' ,'I' ,'Y' ,'Y', 'E', 'Yu' ,'Ya' );

    $textcyr = str_replace($cyr, $lat, $textcyr);
    $textlat = str_replace($lat, $cyr, $textlat);
    echo("$textcyr $textlat");

J'ai préféré ё = e, ъ = i, ы = y et э = e parce que je l'utilise de cette façon.

0voto

Denis Rudov Points 505

Pour moi, la meilleure solution était d'utiliser

strtr("Информация",array('И'=>'I','н'=>'n','ф'=>'f', ...and so on... ))

0voto

$textcyr = 'Њушка Ћушка Љубав Ђато ђата части ';

$textlat = 'Ljubav njuška džoša Džoša';
$textlat = str_replace("nj","њ",$textlat);
$textlat = str_replace("Nj","Њ",$textlat);
$textlat = str_replace("lj","љ",$textlat);
$textlat = str_replace("Lj","Љ",$textlat);
$textlat = str_replace("dž","џ",$textlat);
$textlat = str_replace("Dž","Џ",$textlat);

$textcyr = str_replace($cyr, $lat, $textcyr);
$textlat = str_replace($lat, $cyr, $textlat);

echo $textcyr;
echo $textlat;

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X