【问题标题】:php, iconv() function error messagephp, iconv() 函数错误信息
【发布时间】:2015-01-11 18:17:12
【问题描述】:

我的文本文件包含以下字符串:“ãéðä”。 我的 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

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

【问题讨论】:

  • 您确定文件test.txt 是使用ISO-8859-1 编码保存的吗?
  • 代码没有错误。您在文件中输入不正确。使用 Artjoman 的答案

标签: php iconv


【解决方案1】:

你可以使用

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

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

【讨论】:

  • 谢谢,这次输出 eas: 'ÿþãéðä;
  • @Dai,我保存为“ansi”,然后是“UTF-8”,然后是“unicode”,然后是“unicode big endian”。我没有收到错误,但输出仍然是乱码。
  • 还有一个选项 - 使用功能 mb_convert_encoding $utf_8 = mb_convert_encoding($text, "UTF-8", "windows-1251"); 如果您安装了 mb_string 扩展程序
  • 谢谢,我终于找到了解决我的问题的方法。通过 notepad++ 保存文本文件,使用:在没有 BOM 的 UTF-8 中编码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多