htmlspecialchars()和htmlentities()之间的区别是什么?什么时候我应该用一种或另一种?
当前回答
Htmlspecialchars可以被使用:
When there is no need to encode all characters which have their HTML equivalents. If you know that the page encoding match the text special symbols, why would you use htmlentities? htmlspecialchars is much straightforward, and produce less code to send to the client. For example: 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>. // ^ ^ The second one is shorter, and does not cause any problems if ISO-8859-1 charset is set. When the data will be processed not only through a browser (to avoid decoding HTML entities), If the output is XML (see the answer by Artefacto).
其他回答
当你只想让你的字符串是XML和HTML安全的时候,你应该使用htmlspecialchars($strText, ENT_QUOTES):
例如,encode
& to & “To”; < to < >到> '到'
但是,如果文本中还有Unicode或不常见符号的额外字符,则应该使用htmlentities()来确保它们在HTML页面中正确显示。
注:
'只会被htmlspecialchars()编码到'如果传入ENT_QUOTES选项。& # 039;是更安全的使用然后'由于旧版本的Internet Explorer不支持'实体。 从技术上讲,>不需要按照XML规范进行编码,但是为了与<被编码的要求保持一致,通常也会对它进行编码。
htmlspecialchars()进行最少的编码,以确保字符串不会被解析为HTML。这使得您的字符串比使用htmlentities()对所有具有编码的内容进行编码时更易于阅读。
htmlspecialchars()和htmlentities()之间的区别非常小。让我们来看一些例子:
htmlspecialchars
htmlspecialchars(string $string) takes multiple arguments where as the first argument is a string and all other arguments (certain flags, certain encodings etc. ) are optional. htmlspecialchars converts special characters in the string to HTML entities. For example if you have < br > in your string, htmlspecialchars will convert it into < b >. Whereas characters like µ † etc. have no special significance in HTML. So they will be not converted to HTML entities by htmlspecialchars function as shown in the below example.
echo htmlspecialchars('An example <br>'); // This will print - An example < br >
echo htmlspecialchars('µ †'); // This will print - µ †
htmlentities
Htmlentities (string $string)非常类似于htmlspecialchars,需要多个参数,其中第一个参数是字符串,所有其他参数都是可选的(某些标志,某些编码等)。与htmlspecialchars不同,htmlentities不仅将字符串中的特殊字符转换为HTML实体,还将所有适用的字符转换为HTML实体。
echo htmlentities('An example <br>'); // This will print - An example < br >
echo htmlentities('µ †'); // This will print - µ †
PHP htmlentities文档:
这个函数在所有方面都与htmlspecialchars()相同,除了htmlentities(),所有具有HTML字符实体等价的字符都被翻译成这些实体。
从PHP文档htmlspecialchars:
某些字符在HTML中有特殊的意义,如果要保留它们的含义,就应该用HTML实体来表示。这个函数返回一个包含这些转换的字符串;所做的翻译是那些对日常web编程最有用的。如果需要翻译所有HTML字符实体,请使用htmlentities()。
区别在于编码的内容。选项是所有(实体)或“特殊”字符,如&号、双引号和单引号、小于和大于(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>.
// ^ ^
我刚刚发现了get_html_translation_table函数。你传递给它HTML_ENTITIES或者HTML_SPECIALCHARS,它会返回一个数组,里面是要被编码的字符以及它们的编码方式。
推荐文章
- 使用PHP加密和解密密码的最佳方法是什么?
- 如何实现一个好的脏话过滤器?
- PHP中的三个点(…)是什么意思?
- Guzzlehttp -如何从guzzle6得到响应的正文?
- 移动一个文件到服务器上的另一个文件夹
- Laravel中使用雄辩的ORM进行批量插入
- PHP 5.4调用时引用传递-容易修复可用吗?
- 格式化字节到千字节,兆字节,千兆字节
- 如何在PHP中获得变量名作为字符串?
- 用“+”(数组联合运算符)合并两个数组如何工作?
- Laravel PHP命令未找到
- 如何修复从源代码安装PHP时未发现xml2-config的错误?
- 在PHP中对动态变量名使用大括号
- 如何从对象数组中通过对象属性找到条目?
- 如何从关联数组中删除键及其值?