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) =>'&lt;'
'>' (greater than) =>'&gt;'

而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.


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

参考链接

  1. W3Schools.com - Symbols Entities
  2. 这是一个完整的字符实体参考Webstandards.org - Symbol Entities
  3. http://www.neoease.com/using-html-symbol-entities/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值