Convert HTML entities to their corresponding characters html_entity_decode

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

html_entity_decode — Convert HTML entities to their corresponding characters

说明

html_entity_decode(string $string [, int $flags= ent_compat | ent_html401 [, string $encoding= ini_get("default_charset")]]): string

html_entity_decode()is the opposite ofhtmlentities()in that it converts HTML entities in thestringto their corresponding characters.

More precisely, this function decodes all the entities(including all numeric entities)that a)are necessarily valid for the chosen document type — i.e., for XML, this function does not decode named entities that might be defined in some DTD — and b)whose character or characters are in the coded character set associated with the chosen encoding and are permitted in the chosen document type. All other entities are left as is.

参数

string

The input string.

flags

A bitmask of one or more of the following flags, which specify how to handle quotes and which document type to use. The default isENT_COMPAT | ENT_HTML401.

Availableflagsconstants
Constant NameDescription
ENT_COMPATWill convert double-quotes and leave single-quotes alone.
ENT_QUOTESWill convert both double and single quotes.
ENT_NOQUOTESWill leave both double and single quotes unconverted.
ENT_HTML401Handle code as HTML 4.01.
ENT_XML1Handle code as XML 1.
ENT_XHTMLHandle code as XHTML.
ENT_HTML5Handle code as HTML 5.

encoding

An optional argument defining the encoding used when converting characters.

If omitted, the default value of theencodingvaries 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-1ISO8859-1西欧,Latin-1
ISO-8859-5ISO8859-5Little used cyrillic charset(Latin/Cyrillic).
ISO-8859-15ISO8859-15西欧,Latin-9。增加欧元符号,法语和芬兰语字母在 Latin-1(ISO-8859-1)中缺失。
UTF-8 ASCII 兼容的多字节 8 位 Unicode。
cp866ibm866, 866DOS 特有的西里尔编码。本字符集在 4.3.2 版本中得到支持。
cp1251Windows-1251, win-1251, 1251Windows 特有的西里尔编码。本字符集在 4.3.2 版本中得到支持。
cp1252Windows-1252, 1252Windows 特有的西欧编码。
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_charsetand current locale(seenl_langinfo()andsetlocale()), in this order. Not recommended.
其他字符集没有认可。将会使用默认编码并抛出异常。

淘宝 adsense 商品推荐

谷歌 admob Adsense 代收Pin码 美国 香港 丰富地址代收 商务服务

价格:50元

返回值

Returns the decoded string.

更新日志

版本说明
5.6.0The default value for theencodingparameter was changed to be the value of thedefault_charsetconfiguration option.
5.4.0Default encoding changed from ISO-8859-1 to UTF-8.
5.4.0The constantsENT_HTML401,ENT_XML1,ENT_XHTMLandENT_HTML5were added.

范例

Example #1 Decoding HTML entities

<?php
$orig = "I'll \"walk\" the <b>dog</b> now";

$a = htmlentities($orig);

$b = html_entity_decode($a);

echo $a; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now

echo $b; // I'll "walk" the <b>dog</b> now
?>

注释

You might wonder why trim(html_entity_decode('&nbsp;')); doesn't reduce the string to an empty string, that's because the '&nbsp;' entity is not ASCII code 32(which is stripped bytrim())but ASCII code 160(0xa0)in the default ISO 8859-1 encoding.

淘宝 云主机 商品推荐

远程电脑出租E3E5物理双路服务器租用云主机渲染模拟器虚拟机多开

价格:19元

参见

htmlentities()- 将字符转换为 HTML 转义字符

htmlspecialchars()- 将特殊字符转换为 HTML 实体

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

urldecode()- 解码已编码的 URL 字符串