0

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 !

deotpit
  • 95
  • 1
  • 3
  • 12

1 Answers1

4

You can use

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

It will ignore illegal characters and remove them.

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
Artjoman
  • 286
  • 1
  • 9
  • Thanks, this time the output eas: 'ÿþãéðä; – deotpit Nov 14 '14 at 17:09
  • @Dai, I saved as "ansi" then "UTF-8", then "unicode", then "unicode big endian". I didn't get errors but the output remained gibrish. – deotpit Nov 14 '14 at 17:21
  • 1
    There is one more option - to use function `mb_convert_encoding` `$utf_8 = mb_convert_encoding($text, "UTF-8", "windows-1251");` In case you have `mb_string` extension installed – Artjoman Nov 19 '14 at 08:38
  • Thanks, I finally found the solution to my problem by. saving the text file via notepad++ using: Encode in UTF-8 without BOM. – deotpit Nov 19 '14 at 20:23