使用带有PHP的mb_detect_encoding()时,字符编码UTF8问题

时间:2011-04-12 11:59:42

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

我正在阅读RSS Feed http://beersandbeans.com/feed/

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

为什么输出Â?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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()