问题
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
回答1:
The illegal character is not in $matches[1]
, but in $xml
Try
iconv($matches[1], 'utf-8//TRANSLIT', $xml);
And showing us the input string would be nice for a better answer.
回答2:
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
$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:
echo iconv("cp1252", "utf-8//IGNORE", $cp1252);
回答3:
BE VERY CAREFUL, the problem may come from multibytes encoding and inappropriate PHP functions used...
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):
$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.
The appropriate way is to use Multibyte String Functions mb_strtolower()
instead of 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
PHP Manual on the Multibyte String
回答4:
I found one Solution :
echo iconv('UTF-8', 'ASCII//TRANSLIT', utf8_encode($string));
use utf8_encode()
来源:https://stackoverflow.com/questions/8727735/iconv-detected-an-illegal-character-in-input-string