php, iconv() function error message

3.5k views Asked by At

My text file contains the following string: "ãéðä". My PHP batch to convert that string goes like this:

<?php
$text = iconv("UTF-8","ISO-8859-1", "ãéðä");
echo $text;
?>

When I run the code I get the converted text I want . In a case i have more then one such string, I use the following code:

<?php
$myFile = 'test.txt';
$myHandle = fopen($myFile,'r');
$myText = fread($myHandle, filesize($myFile));
$ridComma = explode(',',$myText);
foreach($ridComma as $item)
{
$text = iconv("UTF-8","ISO-8859-1", $item);
}
fclose($myHandle);
?>

This time I get the following error:

Notice: iconv(): Detected an illegal character in input string in C:\xampp\htdocs\test\test.php on line 8

With the same conversion function I get the conversion in one batch and an error in the other ! Could anyone explain me why? Thanks !

1

There are 1 answers

4
Artjoman On

You can use

$text = iconv('ISO-8859-1', 'UTF-8//IGNORE', $item);

It will ignore illegal characters and remove them.