Cyrillic transliteration in PHP

前端 未结 13 1442
心在旅途
心在旅途 2020-12-05 06:56

How to transliterate cyrillic characters into latin letters?

E.g. Главная страница -> Glavnaja stranica

This Transliteration PHP Extensi

相关标签:
13条回答
  • 2020-12-05 07:21

    $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;
    
    0 讨论(0)
  • 2020-12-05 07:24

    The best option is using PHP Intl Extension. You might want install it first.

    This will do the trick:

    $transliteratedString = transliterator_transliterate('Russian-Latin/BGN', $cyrillicString);
    

    I applied 'Russian-Latin/BGN' because the asker used Russian language in his question. However, there are options for other languages written in the Cyrillic script. To view all of them do this:

    print_r(transliterator_list_ids());
    
    0 讨论(0)
  • 2020-12-05 07:29

    Try following code

    $textcyr="Тествам с кирилица";
            $textlat="I pone dotuk raboti!";
            $cyr = [
                'а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п',
                'р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я',
                'А','Б','В','Г','Д','Е','Ё','Ж','З','И','Й','К','Л','М','Н','О','П',
                'Р','С','Т','У','Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ы','Ь','Э','Ю','Я'
            ];
            $lat = [
                'a','b','v','g','d','e','io','zh','z','i','y','k','l','m','n','o','p',
                'r','s','t','u','f','h','ts','ch','sh','sht','a','i','y','e','yu','ya',
                'A','B','V','G','D','E','Io','Zh','Z','I','Y','K','L','M','N','O','P',
                'R','S','T','U','F','H','Ts','Ch','Sh','Sht','A','I','Y','e','Yu','Ya'
            ];
            $textcyr = str_replace($cyr, $lat, $textcyr);
            $textlat = str_replace($lat, $cyr, $textlat);
            echo("$textcyr $textlat");
    
    0 讨论(0)
  • 2020-12-05 07:29

    I wrote a full transliteration class for all European languages for utf-8. May help (comments are in polish but there isn't a lot of them so here's a few hints:

    1. numbers stored in constants are idCountry in local databse - you change them as you like.
    2. "Rób transliterację dla " means "do transliteration for " - you determine country by const name.
    3. "Słownik tłumaczący rosyjską cyrylicę wg standardu " means "dictionary with transliteration by standard "
    4. "Tablica wycinająca akcenty z różnych znaków narodowych pobrana z http://stuffofinterest.com/misc/utf8-about.html" means "Array to cut off accents from different languages" (it might help if you find some errors in iconv (or cannot use it for some reason).
    5. Methods utf2ascii and cyr2lat are pretty obvious.

    Hope it will help a few people 'cause implementing it was a nightmare :)

    Edit: I just noticed that part of the code is missing so I've put the full class on Pastie: class

    0 讨论(0)
  • 2020-12-05 07:29

    for me the best solution was to use

    strtr("Информация",array('И'=>'I','н'=>'n','ф'=>'f', ...and so on... ))
    
    0 讨论(0)
  • 2020-12-05 07:32

    Here is a function that I use for cleaning characters on Bosnian,Croatian,Serbian latin

     function cleanUTF($name){
            $name = str_replace(array('š','č','đ','č','ć','ž','ñ'),array('s','c','d','c','c','z','n'), $name);
            $name = str_replace(array('Š','Č','Đ','Č','Ć', 'Ž','Ñ'),array('S','C','D','C','C','Z','N'), $name);
            $name = str_replace(array('а','б','в','г','д','е','ё','ж','з','и','й','к','л','љ','м','н','њ','о','п','р','с','т','у','ф','х','ц','ч','џ','ш','щ','ъ','ы','ь','э','ю','я','А','Б','В','Г','Д','Е','Ё','Ж','З','И','Й','К','Л','Љ','М','Н','Њ','О','П','Р','С','Т','У','Ф','Х','Ц','Ч','Џ','Ш','Щ','Ъ','Ы','Ь','Э','Ю','Я'),
                                array('a','b','v','g','d','e','e','z','z','i','j','k','l','lj','m','n','nj','o','p','r','s','t','u','f','h','c','c','dz','s','s','i','j','j','e','ju','ja','A','B','V','G','D','E','E','Z','Z','I','J','K','L','Lj','M','N','Nj','O','P','R','S','T','U','F','H','C','C','Dz','S','S','I','J','J','E','Ju','Ja'), $name);
            return $name;
        }
    
    0 讨论(0)
提交回复
热议问题