htmlentities() - 将字符转换为 HTML 转义字符 - php 字符串函数
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。
常量名 | 描述 |
---|---|
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 处理代码。 |
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-1 | ISO8859-1 | 西欧,Latin-1 |
ISO-8859-5 | ISO8859-5 | Little used cyrillic charset (Latin/Cyrillic). |
ISO-8859-15 | ISO8859-15 | 西欧,Latin-9。增加欧元符号,法语和芬兰语字母在 Latin-1(ISO-8859-1)中缺失。 |
UTF-8 | ASCII 兼容的多字节 8 位 Unicode。 | |
cp866 | ibm866, 866 | DOS 特有的西里尔编码。本字符集在 4.3.2 版本中得到支持。 |
cp1251 | Windows-1251, win-1251, 1251 | Windows 特有的西里尔编码。本字符集在 4.3.2 版本中得到支持。 |
cp1252 | Windows-1252, 1252 | Windows 特有的西欧编码。 |
KOI8-R | koi8-ru, koi8r | 俄语。本字符集在 4.3.2 版本中得到支持。 |
BIG5 | 950 | 繁体中文,主要用于中国台湾省。 |
GB2312 | 936 | 简体中文,中国国家标准字符集。 |
BIG5-HKSCS | 繁体中文,附带香港扩展的 Big5 字符集。 | |
Shift_JIS | SJIS, 932 | 日语 |
EUC-JP | EUCJP | 日语 |
MacRoman | Mac 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.0 | The 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_SUBSTITUTE 、ENT_DISALLOWED 、ENT_HTML401 、ENT_XML1 、ENT_XHTML 、ENT_HTML5 。 |
5.3.0 | 增加常量ENT_IGNORE 。 |
5.2.3 | 增加参数$double_encode。 |
范例
Example #1htmlentities()例子
Example #2ENT_IGNORE
用法示例
参见
html_entity_decode()
Convert HTML entities to their corresponding charactersget_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: <b>I am cool</b> 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
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!