Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What factors influence a successful iconv() TRANSLIT conversion?

Tags:

php

iconv

I am trying to determine what environmental or other factors have an influence on the result of a call to iconv() with the TRANSLIT option.

The following code has different results for me locally when run through Apache and CLI.

<?php
    setlocale(LC_ALL, 'en_GB.UTF-8');
    header('Content-type: text/html; charset=utf-8'); // for web     

    $utf8_string = "Pádraig's naïve café";

    echo iconv('UTF-8', 'ASCII//IGNORE//TRANSLIT', $utf8_string);
?>

Expected result: Padraig's naive cafe

Result in a web browser: (empty string)

Result from CLI: P'adraig's na"ive cafe

On some systems, I do get the expected result, but I can't tie down exactly why.

What factors influence the conversion, and what steps should I follow to increase the chance of a good result?

like image 730
drewm Avatar asked Mar 19 '12 14:03

drewm


1 Answers

The locale is influencing iconv transliteration. However, you should read the warning on the setlocale­Docs manual page:

The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale().

So you might set the locale but it's changed somewhere else. As long as the locale is exactly the same you will get the same results.

You find the documentation and the source-code of iconv here: http://www.gnu.org/software/libiconv/ - that's normally the library used by PHP.

like image 81
hakre Avatar answered Nov 18 '22 14:11

hakre