简体   繁体   中英

php, iconv() function error message

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 !

You can use

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

It will ignore illegal characters and remove them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM