简体   繁体   English

iconv_strlen()-// IGNORE不起作用

[英]iconv_strlen() - //IGNORE not work

In one script i founded a error from iconv_strlen() function. 在一个脚本中,我从iconv_strlen()函数中发现了一个错误。 It try check utf8-len of string in cp1251. 它尝试检查cp1251中字符串的utf8-len。

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

I try use "utf-8//IGNORE" for mute error, but it do not work. 我尝试对静音错误使用“ utf-8 // IGNORE”,但是它不起作用。 Here is example with iconv (//IGNORE work) and iconv_strlen (//IGNORE not work) 这是iconv(// IGNORE正常)和iconv_strlen(// IGNORE无效)的示例

<?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 PHP注意:iconv_strlen():在第9行的/home/user/tmp/test.php中的输入字符串中检测到非法字符PHP堆栈跟踪: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 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. 这是因为iconv_strlen()中的charset参数用于输入字符串,而“ // IGNORE”标志仅用于iconv()调用中的转换过程中的输出字符集。

"//IGNORE" means the characters that cannot be represented in the output charset will be discarded. “ // IGNORE”表示无法在输出字符集中表示的字符将被丢弃。 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. 但是在您的情况下,您给的输入字符串是无效的UTF-8,并告诉iconv_strlen()它是用UTF-8字符集编码的,因此您正确地收到了一条通知,通知您输入字符串包含非法字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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