2

I use this function to transliterate Cyrillic words into Latin:

$string = transliterator_transliterate('Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC;', $name);

However, I get single-letter matches instead of composite ones. That is, I get the word "Финиш" after processing "Finis" and it should be "Finish"

For example (in parentheses is written what should be according to the standard)

ш -> s (sh)
щ -> s (shch)
ч -> c (ch)
.... and other

For example full right table:

а-a б-b в-v г-g д-d е-e ё-e ж-zh з-z и-i й-i к-k л-l м-m н-n о-o п-p р-r
с-s т-t у-u ф-f х-kh ц-ts ч-ch ш-sh щ-shch ы-y ъ-ie э-e ю-iu я-ia 

As I understand it, you need to configure it somewhere in the rules, but I can't Figure out how to do it in the documentation.

Or perhaps there is some other option?

4
  • even the manual has a couple of comments with the single-letter transliteration of Russian. Doesn't make any sense :-( Oct 19, 2020 at 9:38
  • @WalterTross So you think it's easier to write your own function?
    – Alexandr
    Oct 19, 2020 at 9:43
  • If I were you I would read thoroughly the documentation and also wait for an answer here. If both fail in a couple of days I would probably go with str_replace(). Oct 19, 2020 at 9:48
  • @WalterTross preg_replace() could be better than str_replace() for this?
    – JosefZ
    Oct 19, 2020 at 17:54

1 Answer 1

0

All you have to do is to write rules for the particular cases:

$str = 'а-a б-b в-v г-g д-d е-e ё-e ж-zh з-z и-i й-i к-k л-l м-m н-n о-o п-p р-r
с-s т-t у-u ф-f х-kh ц-ts ч-ch ш-sh щ-shch ы-y ъ-ie э-e ю-iu я-ia    Финиш';

$rules = <<<'RULES'
:: NFC ;
ё > e; ж > zh; й > i; х > kh; ц > ts; ч > ch; ш > sh; щ > shch; ъ > ie;
э > e; ю > iu; я > ia;
:: Cyrillic-Latin ;
RULES;

$tls = Transliterator::createFromRules($rules);

echo $tls->transliterate($str), PHP_EOL;

Note that the "particular rules" have to be before the general rule (Cyrillic-Latin).

2
  • Yes, this is it. I have updated my blog post dedicated the the issue, thank you. NOTE: Official documentation about Transliterator::createFromRules lacks the rules code sample. This function is currently not documented; only its argument list is available. (but where is the argument list?) Nov 21, 2021 at 9:00
  • 1
    @JasomDotnet: you can find a documentation here. Nov 21, 2021 at 17:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.