[Solved] iconv – Detected an illegal character in input string

I don’t see anything illegal – any suggestions on what might be the problem?

    if (strtolower($matches[1]) != 'utf-8') {
        $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

Enquirer: Webnet


Solution #1:

The illegal character is not in $matches[1], but in $xml


iconv($matches[1], 'utf-8//TRANSLIT', $xml);

And showing us the input string would be nice for a better answer.

Respondent: Ranty

Solution #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:

$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);
Respondent: NobleUplift

Solution #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 in strtolower()
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

Respondent: micaball

Solution #4:

PHP 7.2

iconv('UTF-8', 'ASCII//TRANSLIT', 'é@ùµ$`à');
// "[email protected]$`a"

iconv('UTF-8', 'ASCII//IGNORE', 'é@ùµ$`à');
// "@$`"

iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', 'é@ùµ$`à');
// "[email protected]$`a"

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', 'é@ùµ$`à');
// "[email protected]$`a"

iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', Transliterator::create('Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC')->transliterate('é@ùµ$`à'))
// "[email protected]$`a" -> same as PHP 7.2
Respondent: atmacola

Solution #5:

I found one Solution :

echo iconv('UTF-8', 'ASCII//TRANSLIT', utf8_encode($string));

use utf8_encode()

Respondent: Irshad Khan

Solution #6:

this bellow solution worked for me


iconv("cp1252", "utf-8//IGNORE", $result_encr);
Respondent: Abdifatah Ciilka

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.