php, iconv() 函数错误消息


php, iconv() function error message

我的文本文件包含以下字符串:"ãéðä"。我用于转换该字符串的 PHP 批处理如下所示:

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

当我运行代码时,我得到了我想要的转换文本。在我有多个这样的字符串的情况下,我使用以下代码:

<?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);
?>

这次我收到以下错误:

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

使用相同的转换函数,我在一个批次中得到转换,在另一个批次中获得错误!谁能解释我为什么?谢谢!

您可以使用

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

它将忽略非法字符并将其删除。