Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iconv - Detected an illegal character in input string

Tags:

php

iconv

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

like image 680
Webnet Avatar asked Jan 04 '12 13:01

Webnet


2 Answers

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);
like image 199
NobleUplift Avatar answered Nov 11 '22 01:11

NobleUplift


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.

like image 38
Ranty Avatar answered Nov 11 '22 03:11

Ranty