htmlentities()与htmlspecialchars() [英] htmlentities() vs. htmlspecialchars()
问题描述
htmlspecialchars()
和htmlentities()
之间有什么区别.什么时候应该使用其中一个?
What are the differences between htmlspecialchars()
and htmlentities()
. When should I use one or the other?
推荐答案
摘自PHP文档中的 html实体 :
From the PHP documentation for htmlentities:
此功能在所有方面都与
htmlspecialchars()
相同,除了htmlentities()
以外,所有具有HTML字符实体等效项的字符都将转换为这些实体.
This function is identical to
htmlspecialchars()
in all ways, except withhtmlentities()
, all characters which have HTML character entity equivalents are translated into these entities.
来自 htmlspecialchars 的PHP文档:
某些字符在HTML中具有特殊的意义,如果要保留其含义,则应由HTML实体表示.该函数返回一个字符串,其中包含一些转换.所做的翻译对于日常Web编程最有用.如果您需要翻译所有HTML字符实体,请改用
htmlentities()
.
不同之处在于所编码的内容.选择是所有(实体)或特殊"字符,例如&"号,双引号和单引号,小于和大于(特殊字符).
The difference is what gets encoded. The choices are everything (entities) or "special" characters, like ampersand, double and single quotes, less than, and greater than (specialchars).
我希望尽可能使用htmlspecialchars
.
例如:
echo htmlentities('<Il était une fois un être>.');
// Output: <Il était une fois un être>.
// ^^^^^^^^ ^^^^^^^
echo htmlspecialchars('<Il était une fois un être>.');
// Output: <Il était une fois un être>.
// ^ ^
这篇关于htmlentities()与htmlspecialchars()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!