PHP html_entity_decode() 函数

实例

把 HTML 实体转换为字符:

<?php
$str = "<© W3Sçh°°¦§>";
echo html_entity_decode($str);
?>

以上代码的 HTML 输出如下(查看源代码):

!DOCTYPE html
<html>
<body>
<? W3S?h????>
</body>
</html>

以上代码的浏览器输出:

<? W3S?h????>

定义和用法

html_entity_decode() 函数把 HTML 实体转换为字符。

html_entity_decode() 函数是 htmlentities() Inverse function of the function.

Syntax

html_entity_decode(string,flags,character-set)
Parameter Description
string Required. Specifies the string to be decoded.
flags

Optional. Specifies how quotes are handled and which document type is used.

Available quote types:

  • ENT_COMPAT - Default. Decode double quotes only.
  • ENT_QUOTES - Decode both double and single quotes.
  • ENT_NOQUOTES - Do not decode any quotes.

Additional flags for specifying the document type used:

  • ENT_HTML401 - Default. Processed as HTML 4.01.
  • ENT_HTML5 - Processed as HTML 5.
  • ENT_XML1 - Processed as XML 1.
  • ENT_XHTML - Processed as XHTML.
character-set

Optional. String value, specifies the character set to be used.

Allowed values:

  • UTF-8 - Default. 8-bit Unicode compatible multi-byte ASCII
  • ISO-8859-1 - Western Europe
  • ISO-8859-15 - Western Europe (includes euro symbol + missing French and Finnish letters in ISO-8859-1)
  • cp866 - Cyrillic character set for DOS
  • cp1251 - Cyrillic character set for Windows
  • cp1252 - Western European character set for Windows
  • KOI8-R - Russian
  • BIG5 - Traditional Chinese, mainly used in Taiwan
  • GB2312 - Simplified Chinese, national standard character set
  • BIG5-HKSCS - Big5 with Hong Kong extension
  • Shift_JIS - Japanese
  • EUC-JP - Japanese
  • MacRoman - Character set used by Mac operating system

Note:In versions before PHP 5.4, unrecognized character sets will be ignored and replaced by ISO-8859-1. Starting from PHP 5.4, unrecognized character sets will be ignored and replaced by UTF-8.

Technical details

Return value: Return the converted string
PHP version: 4.3.0+

Update log:

Version Description
PHP 5 character-set The default parameter value is changed to UTF-8.
PHP 5.4

New additional flags for specifying the document type applicable to the translation table were added:

  • ENT_HTML401
  • ENT_HTML5
  • ENT_XML1
  • ENT_XHTML
PHP 5.3.4 新增了对多字节编码的支持。

更多实例

例子 1

把 HTML 实体转换为字符:

<?php
$str = "Bill & 'Steve'";
echo html_entity_decode($str, ENT_COMPAT); // 只转换双引号
echo "<br>";
echo html_entity_decode($str, ENT_QUOTES); // 转换双引号和单引号
echo "<br>";
echo html_entity_decode($str, ENT_NOQUOTES); // 不转换任何引号
?>

以上代码的 HTML 输出(查看源代码):

!DOCTYPE html
<html>
<body>
Bill & 'Steve'<br>
Bill & 'Steve'<br>
Bill & 'Steve'
</body>
</html>

以上代码的浏览器输出:

Bill & 'Steve'
Bill & 'Steve'
Bill & 'Steve'

例子 2

通过使用西欧字符集,把 HTML 实体转换为字符:

<?php
$str = "My name is Øyvind Åsane. I'm Norwegian.";
echo html_entity_decode($str, ENT_QUOTES, "ISO-8859-1");
?>

以上代码的 HTML 输出(查看源代码):

!DOCTYPE html
<html>
<body>
My name is ?yvind ?sane. I'm Norwegian.
</body>
</html>

以上代码的浏览器输出:

My name is ?yvind ?sane. I'm Norwegian.