FixiDens
FixiDens

Reputation: 29

Transliteration back and forth, does not work correctly

In a long search and trial, I wrote just such a function that works halfway, I managed to fix only the letter Ы.

function translit($str) {
    $str = mb_strtolower($str);
    $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', '', 'j', '', 'ea', 'yu', 'ya', '_'
    ];
    return preg_match('/[а-яё]/iu', $str) ? str_replace($cyr, $lat, $str) : str_replace($lat, $cyr, $str);
}

But it still does not work correctly, for example, a word in Russian Обучение.

If you write like this:

$tutorial = translit('Обучение');
var_dump($tutorial); // string(9) "Obuchenie" 
$tutorial = translit($tutorial);
var_dump($tutorial); // string(17) "Обуcхение" (not true, must be "Обучение")

I understand that this is because the replacement takes place before the same ya will never, since y will be replaced earlier and the occurrence of ya will no longer be.

Yes, but! How can I fix this now?

Upvotes: 0

Views: 35

Answers (1)

Illya
Illya

Reputation: 1275

You can sort by latin lenght and from Consonant to Vocal

function translit($str) {
    $str = mb_strtolower($str);
    $cyr = [
        // 
        'щ',
        //
        'ш',
        'ч',
        'ц',
        'ж',
        //
        'б', 
        'в', 
        'г', 
        'д',
        'з',
        'к', 
        'л', 
        'м', 
        'н',
        'п',
        'р', 
        'с', 
        'т',
        'ф', 
        'х',
        'ы',
        ' ',
        //
        'э',
        'ё',
        'ю',
        'я',
        //
        'а',
        'и',
        'й',
        'у',
        'е',
        'о',
        //
        'ъ', 
        'ь'
    ];
    $lat = [
        // Three Letters
        'sht',
        
        // Two Letters
        'sh', 
        'ch',
        'ts',
        'zh',
        
        // Consonant
        'b',
        'v', 
        'g', 
        'd', 
        'z', 
        'k', 
        'l', 
        'm', 
        'n',  
        'p',
        'r', 
        's', 
        't',  
        'f', 
        'h', 
        'j', 
        '_',
        
        // Two Letters Vocal
        'ea',
        'io',
        'yu',
        'ya',
        
        // One Letter Vocal
        'a',
        'i',
        'y',
        'u',
        'e',
        'o',
        
        // Sign
        '',
        '',
        
    ];
    return preg_match('/[а-яё]/iu', $str) ? str_replace($cyr, $lat, $str) : str_replace($lat, $cyr, $str);
}

$tutorial = translit('Обучение');
var_dump($tutorial); // string(9) "Obuchenie" 
$tutorial = translit($tutorial);
var_dump($tutorial); // string(16) "обучение" 

Upvotes: 2

Related Questions