Reputation: 62384
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
Upvotes: 47
Views: 144168
Reputation: 140
I had the same error with files with "é" characters in ASCII format generated by Notepad++
$row_tmp[$index]=iconv("ASCII", "UTF-8//TRANSLIT",$value);
Using
$row_tmp[$index]=iconv("ISO-8859-1", "UTF-8//TRANSLIT",$value);
...seems to solve it (using a Windows computer with Belgian locale). iconv apparently needs the exact code of the ASCII extension for the source encoding. mb_detect_encoding returns "ASCII" for the same file, so be careful if the source encoding parameter comes from a variable...
Upvotes: 1
Reputation: 11
this bellow solution worked for me
$result_encr="##Sƒ";
iconv("cp1252", "utf-8//IGNORE", $result_encr);
Upvotes: 1
Reputation: 387
iconv('UTF-8', 'ASCII//TRANSLIT', 'é@ùµ$`à');
// "e@uu$`a"
iconv('UTF-8', 'ASCII//IGNORE', 'é@ùµ$`à');
// "@$`"
iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', 'é@ùµ$`à');
// "e@uu$`a"
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"
iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', Transliterator::create('Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC')->transliterate('é@ùµ$`à'))
// "e@uu$`a" -> same as PHP 7.2
Upvotes: 17
Reputation: 1760
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 examples of this issue are available at this SO answer
PHP Manual on the Multibyte String
Upvotes: 20
Reputation: 6046
I found one Solution :
echo iconv('UTF-8', 'ASCII//TRANSLIT', utf8_encode($string));
use utf8_encode()
Upvotes: 2
Reputation: 6015
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);
Upvotes: 57
Reputation: 3362
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.
Upvotes: 38