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 @?

Answers


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.


Need Your Help

How can I implement garbage collection in a multithreaded environment?

linux multithreading memory-management garbage-collection x86

How should I perform garbage collection in a program that consists of multiple threads or processes?

Best way to reuse code at unit testing in javascript

javascript node.js unit-testing mocha code-reuse

I'm using buster.js as the test runner. The basic test is something like: