将特殊字符转换为 HTML 实体,为什么或为什么不呢?
我想知道,最好的做法是什么。将所有 utf-8 特殊字符转换为 HTML 实体或仅转义 &, <和>。
我正在开发几个 PHP 项目。谷歌在我网站的随机部分显示一些错误的 utf-8 结果。
我认为这是由于以下两个原因之一或全部造成的:
- 我的托管提供商没有自动发送编码标头(我已经解决了这个问题)。
- 或者在描述中文本没有完全转义这一事实。
除此之外,我注意到大多数市长公司网站都不会发送 ' 行,并且不会转义字符。
转义所有字符与仅转义最少必要字符是否有缺点(或优点)?
I was wondering, what is the best practice. To convert all utf-8 special characters into HTML entities or only to escape &, < and >.
I'm working on several PHP projects. And google is displaying some wrong utf-8 results for a random part of my website.
I think this is because of one or both of the two following reasons:
- My hosting provider didn't automatically send the encoding headers (I already fixed this).
- Or the fact that in the description the text was not fully escaped.
Besides that, I noticed that most of the mayor company websites don't send the '<?xml version'
line and they don't escape their characters.
Are there downsides (or upsides) to escaping all characters vs only the minimum necessary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
转换
<>&"'
之外的任何字符(由htmlspecialchars() 完成)
)现在已经没有必要了,如果页面的字符集配置正确,那么使用本机 UTF-8 字符(或者您选择的任何字符集)都没有问题。它们有时被用作。这是一种针对字符集问题的误导性解决方法,但这几乎从来都不是一个好主意。Converting any characters beyond
<>&"'
(as done byhtmlspecialchars()
) is not necessary nowadays. If the page's character set is properly configured, it is no problem to use native UTF-8 characters (or whichever character set you choose). Converting them into entities has no advantage. They are sometimes used as a misguided workaround to character set issues, but this is almost never a good idea.