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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With