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

查看:68
本文介绍了htmlentities()与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 with htmlentities(), 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: &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天全站免登陆