Search code examples
phplinuxlocaletranslationiconv

PHP iconv() translates "ä" to "ae" but it should be "a"


So, i've used iconv() to produce URL-safe (and in other contexts as well) versions of swedish words for a long time, so the word Stadsnät becomes Stadsnat but I've just moved to a new server and noticed that it gets translated to Stadsnaet instead, breaking a lot of the links, how do I get iconv() to go back to the "dumber" ä -> a instead of the probably more correct ä -> ae?

It doesn't seem to be a function of PHP, on my develop server it's converted correctly, but not on my production server, and both use the same setlocale() but I guess the locale files in the system may differ.

Any ideas?


Solution

  • I guess you have not specified setLocale correctly.

    with this small example you can see the difference:

    <?php
    $str = 'Stadsnät';
    
    setlocale(LC_ALL, 'en_GB');
    $translit = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
    echo $translit . PHP_EOL;
    // echos Stadsnat
    
    
    setlocale(LC_ALL, 'de_CH');
    $translit = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
    
    echo $translit . PHP_EOL;
    // echos Stadsnaet
    

    try this small script on both servers to be sure.

    EDIT:

    Based on the conversation in the comment: setLocale(LC_ALL, 'en_US.UTF8') solved the problem.

    As an alternative the following Code should do the same:

    transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0080-\uffff] remove', 'Stadsnät')