PHP htmlspecialchars 和字符集问题
我有一个从数据库加载内容的页面。我将页面设置为使用 utf-8,因此
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
我已将数据库上的排序规则设置为 utf8_unicode_ci,并使用以下方法从 mysql 访问该字段:
$desc = htmlspecialchars($row['description'],ENT_QUOTES,'UTF-8');
我这样做是为了修复智能引号和破折号。但是,包含这些字符的任何字段现在都将返回空白。如果我在 htmlspecialchars 调用中去掉“UTF-8”参数,我会得到全文,其中引号应该是菱形字符中的问号。我有什么遗漏的吗?
I've got a page that loads content from a database. I have the page set to use utf-8 as such
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
I've set the collation on the db to utf8_unicode_ci, and I access the field from mysql using this:
$desc = htmlspecialchars($row['description'],ENT_QUOTES,'UTF-8');
I've done this to fix smart quotes and em-dashes. However, any field that contains those characters is being returned blank now. If I take away the 'UTF-8' parameter in my htmlspecialchars call I get the full text just with questionmark-in-a-diamond characters where the quotes should be. Is there something I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果返回空字符串,则可能意味着数据库内容中存在无效的 UTF-8 字符。您还可以设置 ENT_IGNORE 标志,但我建议对有问题的字符串进行二分搜索,以尝试准确找出有问题的字符是什么。
http://www.php.net/manual/en/function.htmlspecialchars.php 。请参阅返回值下的部分。
If you're getting an empty string back, it probably means there's an invalid UTF-8 character in the content from the database. You could also set the ENT_IGNORE flag, but I'd recommend doing a binary search with the offending string to try to figure out exactly what the offending character is.
http://www.php.net/manual/en/function.htmlspecialchars.php. See the section under return value.