unicode - PHP: Why any non-latin char in iconv gives me "illegal character" error? -


for example:

$text = "пд"; echo 'plain    : ', iconv("utf-8", "us-ascii//translit", $text), php_eol; 

outputs

plain : notice: iconv() [function.iconv]: detected illegal character in input string in ... 

i tried add

setlocale(lc_ctype, 'en_us.utf8'); 

but doesn't matter...

you need make sure source file saved in utf-8, not windows-1251. otherwise characters won't representing valid utf-8 sequenses.

update:

right, iconv //translate seems depend on locale. may work correctly if set source language locale. in example, cyrillic locale guess, not 'en_us'.

but in fact if need transliteration 1 language, it's more reliable make simple translation table youself:

$trans = [     'а' => 'a',     'д' => 'd',     'п' => 'p',     ... ]; $translit = str_replace(array_keys($trans), array_values($trans), $source_string); 

but if need work all/unknown languages, have use more complicated such http://php.net/manual/en/class.transliterator.php


Popular posts from this blog