在 PHP 中使用 mb_detect_encoding() 时的字符编码 UTF8 问题
我正在阅读 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
为什么它输出 ?
I am reading an rss feed http://beersandbeans.com/feed/
The feeds says it is UTF8 format, and I am using simplepie rss to import the content When i grab the content and store it in $content
I perform the following:
<?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>
This then produces:
..... Camping: 2,000isk/day for 5 days) = $89 .....
ISO-8859-1
..... Camping: Â Â 2,000isk/day for 5 days) = $89 .....
UTF-8
Why is it outputting the  ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试不指定“UTF-8,ISO-8859-1”并查看它为您提供的编码。它可能正在检测 ISO-8859-1,因为它是该列表中的最后一个,而不是字符串的实际编码。
Try not specifying "UTF-8,ISO-8859-1" and see what encoding it gives you. It might be detecting ISO-8859-1 because it's the last one in that list, rather than the actual encoding of the string.
在
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()
Set strict-mode to true in
mb_detect_encoding()
, see http://www.php.net/manual/de/function.mb-detect-encoding.php#102510Also try http://www.php.net/manual/de/function.mb-convert-encoding.php instead of
iconv()