php iconv - 在输入字符串中检测到非法字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8727735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iconv - Detected an illegal character in input string
提问by Webnet
I don't see anything illegal - any suggestions on what might be the problem?
我没有看到任何违法行为 - 关于可能是什么问题的任何建议?
if (strtolower($matches[1]) != 'utf-8') {
var_dump($matches[1]);
$xml = iconv($matches[1], 'utf-8', $xml);
$xml = str_replace('encoding="'.$matches[1].'"', 'encoding="utf-8"', $xml);
}
Below is my debug/error
以下是我的调试/错误
string(12) "windows-1252"
Notice (8): iconv() [http://php.net/function.iconv]: Detected an illegal character in input string [APP/models/sob_form.php, line 16]
I've verified that the above code is indeed line 16
我已经验证了上面的代码确实是第 16 行
回答by NobleUplift
If you used the accepted answer, however, you will still receive the PHP Notice if a character in your input string cannot be transliterated:
但是,如果您使用了已接受的答案,如果您的输入字符串中的某个字符无法音译,您仍会收到 PHP 通知:
<?php
$cp1252 = '';
for ($i = 128; $i < 256; $i++) {
$cp1252 .= chr($i);
}
echo iconv("cp1252", "utf-8//TRANSLIT", $cp1252);
PHP Notice: iconv(): Detected an illegal character in input string in CP1252.php on line 8
Notice: iconv(): Detected an illegal character in input string in CP1252.php on line 8
So you should use IGNORE, which will ignore what can't be transliterated:
所以你应该使用 IGNORE,它会忽略不能音译的内容:
echo iconv("cp1252", "utf-8//IGNORE", $cp1252);
回答by Ranty
The illegal character is not in $matches[1]
, but in $xml
非法字符不在$matches[1]
,而是在$xml
Try
尝试
iconv($matches[1], 'utf-8//TRANSLIT', $xml);
And showing us the input string would be nice for a better answer.
向我们展示输入字符串将有助于获得更好的答案。
回答by micaball
BE VERY CAREFUL, the problem may come from multibytes encodingand inappropriate PHP functions used...
非常小心,问题可能来自多字节编码和使用不当的 PHP 函数......
It was the case for me and it took me a while to figure it out.
我就是这种情况,我花了一段时间才弄明白。
For example, I get the a string from MySQL using utf8mb4(very common now to encode emojis):
例如,我使用 utf8mb4 从 MySQL获取一个字符串(现在很常见来编码表情符号):
$formattedString = strtolower($stringFromMysql);
$strCleaned = iconv('UTF-8', 'utf-8//TRANSLIT', $formattedString); // WILL RETURN THE ERROR 'Detected an illegal character in input string'
The problem does not stand in
iconv()
but stands instrtolower()
in this case.
在这种情况下,问题不存在,
iconv()
而是存在strtolower()
。
The appropriate way is to use Multibyte String Functionsmb_strtolower()
instead of strtolower()
合适的方法是使用多字节字符串函数mb_strtolower()
而不是strtolower()
$formattedString = mb_strtolower($stringFromMysql);
$strCleaned = iconv('UTF-8', 'utf-8//TRANSLIT', $formattedString); // WORK FINE
MORE INFO
更多信息
More examples of this issue are available at this SO answer
这个问题的更多例子可以在这个SO 答案中找到
PHP Manual on the Multibyte String
多字节字符串的PHP 手册
回答by atmacola
PHP 7.2
PHP 7.2
iconv('UTF-8', 'ASCII//TRANSLIT', 'é@ùμ$`à');
// "e@uu$`a"
iconv('UTF-8', 'ASCII//IGNORE', 'é@ùμ$`à');
// "@$`"
iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', 'é@ùμ$`à');
// "e@uu$`a"
PHP 7.4
PHP 7.4
iconv('UTF-8', 'ASCII//TRANSLIT', 'é@ùμ$`à');
// PHP Notice: iconv(): Detected an illegal character
iconv('UTF-8', 'ASCII//IGNORE', 'é@ùμ$`à');
// "@$`"
iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', 'é@ùμ$`à');
// "e@u$`a"
回答by Irshad Khan
I found one Solution :
我找到了一个解决方案:
echo iconv('UTF-8', 'ASCII//TRANSLIT', utf8_encode($string));
use utf8_encode()
使用 utf8_encode()