【问题标题】:Character Encoding UTF8 Issue when using mb_detect_encoding() with PHP将 mb_detect_encoding() 与 PHP 一起使用时出现字符编码 UTF8 问题
【发布时间】:2011-04-12 11:59:42
【问题描述】:

我正在阅读 rss 提要http://beersandbeans.com/feed/

提要说它是 UTF8 格式,我正在使用 simplepie rss 导入内容当我抓取内容并将其存储在 $content 中时,我执行以下操作:

<?php
header ('Content-type: text/html; charset=utf-8');
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head><body>
<?php
echo $content;
echo $enc = mb_detect_encoding($content, "UTF-8,ISO-8859-1", true);
echo $content = mb_convert_encoding($content, "UTF-8", $enc);
echo $enc = mb_detect_encoding($content, "UTF-8,ISO-8859-1", true);
?>
</body></html>

这会产生:

..... Camping:     2,000isk/day for 5 days) = $89 .....
ISO-8859-1
..... Camping: Â  Â           2,000isk/day for 5 days) = $89 .....
UTF-8

为什么会输出 Â?

【问题讨论】:

    标签: php utf-8 character-encoding multibyte-functions


    【解决方案1】:

    尝试不指定“UTF-8,ISO-8859-1”,看看它给你什么编码。它可能正在检测 ISO-8859-1,因为它是该列表中的最后一个,而不是字符串的实际编码。

    【讨论】:

      【解决方案2】:

      mb_detect_encoding() 中将strict-mode 设置为true,参见http://www.php.net/manual/de/function.mb-detect-encoding.php#102510

      也可以尝试http://www.php.net/manual/de/function.mb-convert-encoding.php 而不是iconv()

      【讨论】:

      • 这两个都试过了,都没有运气:(
      • 嗯,对我有用...请用您的新尝试编辑上面的代码
      • 那么这个呢: echo $content = mb_convert_encoding($content, "UTF-8"); (没有可选的第三个参数)
      • 'strict' 指定是否使用严格编码检测。如果不是在严格模式下,某些字符会被标记错误,因为它们可能出现在多个字符集中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多