大网数据广告活动
PHP 语言与运行手册

get_html_translation_table · 返回使用 htmlspecialchars 和 htmlentities 后的转换表

· 52CE 我爱测

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

get_html_translation_table

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

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

说明

function 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_HTML401 HTML 4.01 表格。
ENT_XML1 XML 1 表格。
ENT_XHTML XHTML 表格。
ENT_HTML5 HTML 5 表格。
encoding

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

支持以下字符集:

支持的字符集列表
字符集 别名 描述
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_charset and current locale (see nl_langinfo() and setlocale()), in this order. Not recommended.

注意:

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

返回值

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

更新日志

版本 说明
8.1.0 flags 从 ENT_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;"
  // ...
}

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。