简体   繁体   中英

iconv_strlen() - //IGNORE not work

In one script i founded a error from iconv_strlen() function. It try check utf8-len of string in cp1251.

$len = iconv_strlen($cp1252str, "utf-8");

I try use "utf-8//IGNORE" for mute error, but it do not work. Here is example with iconv (//IGNORE work) and iconv_strlen (//IGNORE not work)

<?php
$cp1252str = '';

for ($i = 128; $i < 256; $i++) {
    $cp1252str .= chr($i);
}

iconv("cp1252", "utf-8//IGNORE", $cp1252str);
iconv_strlen($cp1252str, "utf-8//IGNORE");

Output:

PHP Notice: iconv_strlen(): Detected an illegal character in input string in /home/user/tmp/test.php on line 9 PHP Stack trace: PHP 1. {main}() /home/user/tmp/test.php:0 PHP 2. iconv_strlen() /home/user/tmp/test.php:9

How can i mute this error? Only with @?

Get answer on https://bugs.php.net/bug.php?id=71346&edit=2

That's because the charset parameter in iconv_strlen() is for the input string, while the "//IGNORE" flag is only intended to be used in the output charset during conversion, in the iconv() call.

"//IGNORE" means the characters that cannot be represented in the output charset will be discarded. But in your case, you are giving an input string that is invalid UTF-8, and telling iconv_strlen() that it is encoded in the UTF-8 charset, so you are correctly receiving a notice that your input string contains an illegal character.

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