get_html_translation_table

(PHP 4, PHP 5, PHP 7, PHP 8)

get_html_translation_table返回使用 htmlspecialchars()htmlentities() 后的转换表

说明

get_html_translation_table(int$table = HTML_SPECIALCHARS, int$flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, string$encoding = "UTF-8"): array

get_html_translation_table() 将返回 htmlspecialchars()htmlentities() 处理后的转换表。

注意:

特殊字符可以使用多种转换方式。 例如: " 可以被转换成 ", " 或者 &#x22. get_html_translation_table() 返回其中最常用的。

参数

table

有两个新的常量 (HTML_ENTITIES, HTML_SPECIALCHARS) 允许你指定你想要的表。

flags

A bitmask of one or more of the following flags, which specify which quotes the table will contain as well as which document type the table is for. The default is ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.

可用的 flags 常量
常量名说明
ENT_COMPAT表格将包含双引号但不包含单引号实体。
ENT_QUOTES表格将包含双引号和单引号实体。
ENT_NOQUOTES表格不包含双引号实体,也不包含单引号实体。
ENT_SUBSTITUTE Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or � (otherwise) instead of returning an empty string.
ENT_HTML401HTML 4.01 表格。
ENT_XML1XML 1 表格。
ENT_XHTMLXHTML 表格。
ENT_HTML5HTML 5 表格。
encoding

要使用的编码。如果省略,则此参数的默认值是 UTF-8。

支持以下字符集:

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

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

返回值

将转换表作为数组返回,原始字符为键,实体为值。

更新日志

版本说明
8.1.0flagsENT_COMPAT 更改为 ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401

示例

示例 #1 转换表示例

<?php
var_dump
(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES | ENT_HTML5));
?>

以上示例的输出类似于:

array(1510) { [" "]=> string(9) "&NewLine;" ["!"]=> string(6) "&excl;" ["""]=> string(6) "&quot;" ["#"]=> string(5) "&num;" ["$"]=> string(8) "&dollar;" ["%"]=> string(8) "&percnt;" ["&"]=> string(5) "&amp;" ["'"]=> string(6) "&apos;"}

参见

To Top