简体   繁体   中英

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 an illegal character in input string in ...

I tried to add

setlocale(LC_CTYPE, 'en_US.UTF8');

but it doesn't matter...

You need to make sure that your source file is actually saved in UTF-8, but not Windows-1251. Otherwise those characters won't be representing valid UTF-8 sequenses.

Update:

Right, the iconv //TRANSLATE seems to depend on locale. It may work correctly if you set it to the source language locale. So in your example, this would be some cyrillic locale I guess, but not 'en_US'.

But in fact if you need transliteration just for one language, it's much more reliable to make a simple translation table youself:

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

But if you need it to work for all/unknown languages, you will have to use something more complicated such as http://php.net/manual/en/class.transliterator.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM