百科狗-知识改变命运!
--

htmlentities() - 将字符转换为 HTML 转义字符 - php 字符串函数

梵高1年前 (2023-11-21)阅读数 14#技术干货
文章标签字符集

htmlentities()

(PHP 4, PHP 5, PHP 7)

将字符转换为 HTML 转义字符

说明

htmlentities(string $string[,int $flags= ENT_COMPAT | ENT_HTML401[,string $encoding= ini_get("default_charset")[,bool $double_encode= true]]]) : string

本函数各方面都和htmlspecialchars()一样,除了htmlentities()会转换所有具有 HTML 实体的字符。

如果要解码(反向操作),可以使用html_entity_decode()。

参数

$string

输入字符。

$flags

以下一组位掩码标记,用于设置如何处理引号、无效代码序列、使用文档的类型。默认是ENT_COMPAT | ENT_HTML401

有效$flags标记常量
常量名描述
ENT_COMPAT会转换双引号,不转换单引号。
ENT_QUOTES既转换双引号也转换单引号。
ENT_NOQUOTES单/双引号都不转换
ENT_IGNORE静默丢弃无效的代码单元序列,而不是返回空字符串。不建议使用此标记,因为它»可能有安全影响。
ENT_SUBSTITUTE替换无效的代码单元序列为 Unicode 代替符(Replacement Character), U+FFFD (UTF-8)或者�(其他),而不是返回空字符串。
ENT_DISALLOWED为文档的无效代码点替换为 Unicode 代替符(Replacement Character): U+FFFD (UTF-8),或�(其他),而不是把它们留在原处。比如以下情况下就很有用:要保证 XML 文档嵌入额外内容时格式合法。
ENT_HTML401以 HTML 4.01 处理代码。
ENT_XML1以 XML 1 处理代码。
ENT_XHTML以 XHTML 处理代码。
ENT_HTML5以 HTML 5 处理代码。
$encoding

An optional argument defining the encoding used when converting characters.

If omitted, the default value of the$encodingvaries depending on the PHP version in use. In PHP 5.6 and later,thedefault_charsetconfiguration option is used as the default value. PHP 5.4 and 5.5 will useUTF-8as the default. Earlier versions of PHP useISO-8859-1.

Although this argument is technically optional, you are highly encouraged to specify the correct value for your code if you are using PHP 5.5 or earlier, or if yourdefault_charsetconfiguration option may be set incorrectly for the given input.

支持以下字符集:

支持的字符集列表
字符集别名描述
ISO-8859-1ISO8859-1西欧,Latin-1
ISO-8859-5ISO8859-5Little used cyrillic charset (Latin/Cyrillic).
ISO-8859-15ISO8859-15西欧,Latin-9。增加欧元符号,法语和芬兰语字母在 Latin-1(ISO-8859-1)中缺失。
UTF-8ASCII 兼容的多字节 8 位 Unicode。
cp866ibm866, 866DOS 特有的西里尔编码。本字符集在 4.3.2 版本中得到支持。
cp1251Windows-1251, win-1251, 1251Windows 特有的西里尔编码。本字符集在 4.3.2 版本中得到支持。
cp1252Windows-1252, 1252Windows 特有的西欧编码。
KOI8-Rkoi8-ru, koi8r俄语。本字符集在 4.3.2 版本中得到支持。
BIG5950繁体中文,主要用于中国台湾省。
GB2312936简体中文,中国国家标准字符集。
BIG5-HKSCS繁体中文,附带香港扩展的 Big5 字符集。
Shift_JISSJIS, 932日语
EUC-JPEUCJP日语
MacRomanMac OS 使用的字符串。
''An empty string activates detection from script encoding (Zend multibyte),default_charsetand current locale (seenl_langinfo()andsetlocale()), in this order. Not recommended.

Note:其他字符集没有认可。将会使用默认编码并抛出异常。

$double_encode

关闭$double_encode时,PHP 不会转换现有的 HTML 实体,默认是全部转换。

返回值

返回编码后的字符。

如果指定的编码$encoding里,$string包含了无效的代码单元序列,没有设置ENT_IGNORE或者ENT_SUBSTITUTE标记的情况下,会返回空字符串。

更新日志

版本说明
5.6.0The default value for the$encodingparameter was changed to be the value of thedefault_charsetconfiguration option.
5.4.0$encoding参数的默认值改成 UTF-8。
5.4.0增加常量ENT_SUBSTITUTEENT_DISALLOWEDENT_HTML401ENT_XML1ENT_XHTMLENT_HTML5
5.3.0增加常量ENT_IGNORE
5.2.3增加参数$double_encode。

范例

Example #1htmlentities()例子

htmlentities() - 将字符转换为 HTML 转义字符 - php 字符串函数

Example #2ENT_IGNORE用法示例

参见

  • html_entity_decode()Convert HTML entities to their corresponding characters
  • get_html_translation_table()返回使用 htmlspecialchars 和 htmlentities 后的转换表
  • htmlspecialchars()将特殊字符转换为 HTML 实体
  • nl2br()在字符串所有新行之前插入 HTML 换行标记
  • urlencode()编码 URL 字符串
An important note below about using this function to secure your application against Cross Site Scripting (XSS) vulnerabilities.
When printing user input in an attribute of an HTML tag, the default configuration of htmlEntities() doesn't protect you against XSS, when using single quotes to define the border of the tag's attribute-value. XSS is then possible by injecting a single quote:

I used this for a textarea content for comments. Anyway, note that using the "$res_2" form the function will leave unconverted single/double quotes. At this point you should use str_replace() function to perform the characters but be careful because..
There is a feature when writing to XML using an AJAX call to PHP that rarely is mentioned. I struggled for many hours using htmlentities() because what was getting written to my XML document was not as expected. I naturally assumed that I should be converting my strings before writing them to XML to adhere to XML rules on illegal characters. To my surprise, when converting with htmlentities() or htmlspecialchars() and then writing to an XML file, the resulting ampersands get converted afterwards! Consider the following example:

When you append $str to an XML element and save() the document, you would expect the XML document's source code to look something like this:
<b>I am cool</b>
But that is not what happens. The resulting ampersands get converted by PHP automatically to & and your source code ends up looking like this:
&lt;b&gt;I am cool&lt;/b&gt;
As you can see, this creates problems when trying to output the XML data back to HTML. It is important to remember that when writing to XML this way, special characters like ">" and "

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)