千家信息网

PHP中htmlentities和htmlspecialchars有哪些区别

发表于:2024-10-22 作者:千家信息网编辑
千家信息网最后更新 2024年10月22日,本文小编为大家详细介绍"PHP中htmlentities和htmlspecialchars有哪些区别",内容详细,步骤清晰,细节处理妥当,希望这篇"PHP中htmlentities和htmlspeci
千家信息网最后更新 2024年10月22日PHP中htmlentities和htmlspecialchars有哪些区别

本文小编为大家详细介绍"PHP中htmlentities和htmlspecialchars有哪些区别",内容详细,步骤清晰,细节处理妥当,希望这篇"PHP中htmlentities和htmlspecialchars有哪些区别"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

htmlentities跟htmlspecialchars都是用于将字符串的特殊字符中转成HTML字符实体。只是htmlspecialcharsl转义的特殊字符集只有5个,

'&' (ampersand) => '&'
'"' (double quote) => '"' when ENT_NOQUOTES is not set.
''' (single quote) => ''' only when ENT_QUOTES is set.
'<' (less than) => '<'
'>' (greater than) => '>'

而htmlentities转义的特殊字符集是完整的HTML字符实体集。

对于这两个函数,php手册上都是英文做的解释,其中在htmlentities函数的说明部分有这么一段英文:

This function is identical(完全相同的) to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents(HTML
character entity equivalents译为HTML字符实体等价物) are translated into these entities.

从这句话中我们也可以看出来这两个函数虽然基本功能差不多,但是还是有细微的差别在里面的。再仔细看htmlspecialchars函数里面的一段话:

Certain characters(其实就是'&', 双引号(没有设置ENT_NOQUOTES时才会转义), 单引号(当且仅当设置ENT_QUOTES才会转义), '<', '>') have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function
returns a string with some of these conversions made; the translations made are those most useful for everyday web programming. If you require all HTML character entities to be translated, use htmlentities() instead.

注:使用这两个函数时最后指明编码格式哦。

读到这里,这篇"PHP中htmlentities和htmlspecialchars有哪些区别"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。

0