简体   繁体   中英

iconv - Notice: iconv() [function.iconv]: Detected an illegal character in input string

print iconv('UTF-8', 'us-ascii//TRANSLIT', 'Ин Совет Россия, дог фес ю!');

I don't get it why it throws this notice. The string is a valid UTF8.

It should convert all characters to their ASCII equivalents, right?

For example И should be I , н should be n and so on...

The iconv function does not actually do transliteration; the parameter name TRANSLIT is misleading. The documentation says: “If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters.” Apparently this means just a set of ad hoc mappings, like “€” to “EUR”, and “ß” to “ss”.

For transliteration, you need something different. To transliterate Russian text from Cyrillic letters to Latin letters, the hard part is to decide which transliteration scheme to use. After that, it's just raw work. Something like this, just with added data (arrays that cover the Cyrillic alphabet as used in Russian and the desired transliteration of each letter):

$msg =  'Ин Совет Россия, дог фес ю!';
$cyr = array('и', 'я');
$lat = array('i', 'ya');
$latmsg = str_replace($cyr, $lat, $msg);
print $latmsg;

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