htmlentities() 与 htmlspecialchars() [英] htmlentities() vs. htmlspecialchars()

查看:23
本文介绍了htmlentities() 与 htmlspecialchars()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

htmlspecialchars()htmlentities() 之间有什么区别.我什么时候应该使用其中一种?

What are the differences between htmlspecialchars() and htmlentities(). When should I use one or the other?

推荐答案

来自 htmlentities 的 PHP 文档:

From the PHP documentation for htmlentities:

此函数在所有方面都与 htmlspecialchars() 相同,除了 htmlentities() 之外,所有具有 HTML 字符实体等效项的字符都将转换为这些实体.

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

来自 htmlspecialchars 的 PHP 文档:

From the PHP documentation for htmlspecialchars:

某些字符在 HTML 中具有特殊意义,如果要保留其含义,应由 HTML 实体表示.此函数返回一个字符串,其中进行了一些转换;所做的翻译是那些对日常网络编程最有用的翻译.如果您需要翻译所有 HTML 字符实体,请改用 htmlentities().

Certain characters 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.

区别在于编码的内容.选择是所有内容(实体)或特殊"字符,如与号、双引号和单引号、小于和大于(特殊字符).

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.

I prefer to use htmlspecialchars whenever possible.

例如:

    echo htmlentities('<Il était une fois un être>.');
    // Output: &lt;Il &eacute;tait une fois un &ecirc;tre&gt;.
    //                ^^^^^^^^                 ^^^^^^^

    echo htmlspecialchars('<Il était une fois un être>.');
    // Output: &lt;Il était une fois un être&gt;.
    //                ^                 ^

这篇关于htmlentities() 与 htmlspecialchars()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆