htmlentities

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

htmlentities將字符轉(zhuǎn)換為 HTML 轉(zhuǎn)義字符

說明

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

本函數(shù)各方面都和 htmlspecialchars() 一樣, 除了 htmlentities() 會轉(zhuǎn)換所有具有 HTML 實體的字符。

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

參數(shù)

string

輸入字符。

flags

以下一組位掩碼標(biāo)記,用于設(shè)置如何處理引號、無效代碼序列、使用文檔的類型。 默認(rèn)是 ENT_COMPAT | ENT_HTML401。

有效 flags 標(biāo)記常量
常量名 描述
ENT_COMPAT 會轉(zhuǎn)換雙引號,不轉(zhuǎn)換單引號。
ENT_QUOTES 既轉(zhuǎn)換雙引號也轉(zhuǎn)換單引號。
ENT_NOQUOTES 單/雙引號都不轉(zhuǎn)換
ENT_IGNORE 靜默丟棄無效的代碼單元序列,而不是返回空字符串。 不建議使用此標(biāo)記, 因為它? 可能有安全影響。
ENT_SUBSTITUTE 替換無效的代碼單元序列為 Unicode 代替符(Replacement Character), U+FFFD (UTF-8) 或者 � (其他),而不是返回空字符串。
ENT_DISALLOWED 為文檔的無效代碼點替換為 Unicode 代替符(Replacement Character): U+FFFD (UTF-8),或 �(其他),而不是把它們留在原處。 比如以下情況下就很有用:要保證 XML 文檔嵌入額外內(nèi)容時格式合法。
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, encoding defaults to the value of the default_charset configuration option.

Although this argument is technically optional, you are highly encouraged to specify the correct value for your code if the default_charset configuration 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 兼容的多字節(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)編碼并拋出異常。

double_encode

關(guān)閉 double_encode 時,PHP 不會轉(zhuǎn)換現(xiàn)有的 HTML 實體, 默認(rèn)是全部轉(zhuǎn)換。

返回值

返回編碼后的字符。

如果指定的編碼 encoding 里, string 包含了無效的代碼單元序列, 沒有設(shè)置 ENT_IGNORE 或者 ENT_SUBSTITUTE 標(biāo)記的情況下,會返回空字符串。

更新日志

版本 說明
5.6.0 The default value for the encoding parameter was changed to be the value of the default_charset configuration option.
5.4.0 encoding 參數(shù)的默認(rèn)值改成 UTF-8。
5.4.0 增加常量 ENT_SUBSTITUTEENT_DISALLOWED、 ENT_HTML401、 ENT_XML1、 ENT_XHTML、 ENT_HTML5
5.3.0 增加常量 ENT_IGNORE。
5.2.3 增加參數(shù) double_encode。

范例

示例 #1 htmlentities() 例子

<?php
$str 
"A 'quote' is <b>bold</b>";

// 輸出: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);

// 輸出: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($strENT_QUOTES);
?>

示例 #2 ENT_IGNORE 用法示例

<?php
$str 
"\x8F!!!";

// 輸出空 string
echo htmlentities($strENT_QUOTES"UTF-8");

// 輸出 "!!!"
echo htmlentities($strENT_QUOTES ENT_IGNORE"UTF-8");
?>

參見