get_html_translation_table

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

get_html_translation_table返回使用 htmlspecialchars()htmlentities() 后的轉(zhuǎn)換表

說明

get_html_translation_table(int $table = HTML_SPECIALCHARS, int $flags = ENT_COMPAT | ENT_HTML401, string $encoding = 'UTF-8'): array

get_html_translation_table() 將返回 htmlspecialchars()htmlentities() 處理后的轉(zhuǎn)換表。

注意:

特殊字符可以使用多種轉(zhuǎn)換方式。 例如: " 可以被轉(zhuǎn)換成 ", " 或者 &#x22. get_html_translation_table() 返回其中最常用的。

參數(shù)

table

有兩個(gè)新的常量 (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_COMPAT | ENT_HTML401.

Available flags constants
Constant Name Description
ENT_COMPAT Table will contain entities for double-quotes, but not for single-quotes.
ENT_QUOTES Table will contain entities for both double and single quotes.
ENT_NOQUOTES Table will neither contain entities for single quotes nor for double quotes.
ENT_HTML401 Table for HTML 4.01.
ENT_XML1 Table for XML 1.
ENT_XHTML Table for XHTML.
ENT_HTML5 Table for HTML 5.

encoding

Encoding to use. If omitted, the default value for this argument is ISO-8859-1 in versions of PHP prior to 5.4.0, and UTF-8 from PHP 5.4.0 onwards.

支持以下字符集:

支持的字符集列表
字符集 別名 描述
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 兼容的多字節(jié) 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 簡體中文,中國國家標(biāo)準(zhǔn)字符集。
BIG5-HKSCS   繁體中文,附帶香港擴(kuò)展的 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.

注意: 其他字符集沒有認(rèn)可。將會使用默認(rèn)編碼并拋出異常。

返回值

將轉(zhuǎn)換表作為一個(gè)數(shù)組返回。

更新日志

版本 說明
5.4.0 The default value for the encoding parameter was changed to UTF-8.
5.4.0 The constants ENT_HTML401, ENT_XML1, ENT_XHTML and ENT_HTML5 were added.
5.3.4 The encoding parameter was added.

范例

示例 #1 Translation Table Example

<?php
var_dump
(get_html_translation_table(HTML_ENTITIESENT_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;"
  // ...
}

參見