目录

PHP html_entity_decode() 函数

❮ PHP 字符串参考

示例

将 HTML 实体转换为字符:

<?php
$str = '&lt;a href=&quot;https://www.91xjr.com&quot;&gt;91xjr.com&lt;/a&gt;';
echo html_entity_decode($str);
?>

上述代码的 HTML 输出将是(查看源代码):

<a href="https://www.91xjr.com">91xjr.com</a>

上述代码的浏览器输出将是:



定义和用法

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

html_entity_decode() 函数与html实体()


语法

html_entity_decode( string,flags,character-set)

参数值

Parameter Description
string Required. Specifies the string to decode
flags Optional. Specifies how to handle quotes and which document type to use.

The available quote styles are:

  • ENT_COMPAT - Default. Decodes only double quotes
  • ENT_QUOTES - Decodes double and single quotes
  • ENT_NOQUOTES - Does not decode any quotes

Additional flags for specifying the used doctype:

  • ENT_HTML401 - Default. Handle code as HTML 4.01
  • ENT_HTML5 - Handle code as HTML 5
  • ENT_XML1 - Handle code as XML 1
  • ENT_XHTML - Handle code as XHTML
character-set Optional. A string that specifies which character-set to use.

Allowed values are:

  • UTF-8 - Default. ASCII compatible multi-byte 8-bit Unicode
  • ISO-8859-1 - Western European
  • ISO-8859-15 - Western European (adds the Euro sign + French and Finnish letters missing in ISO-8859-1)
  • cp866 - DOS-specific Cyrillic charset
  • cp1251 - Windows-specific Cyrillic charset
  • cp1252 - Windows specific charset for Western European
  • KOI8-R - Russian
  • BIG5 - Traditional Chinese, mainly used in Taiwan
  • GB2312 - Simplified Chinese, national standard character set
  • BIG5-HKSCS - Big5 with Hong Kong extensions
  • Shift_JIS - Japanese
  • EUC-JP - Japanese
  • MacRoman - Character-set that was used by Mac OS

Note: Unrecognized character-sets will be ignored and replaced by ISO-8859-1 in versions prior to PHP 5.4. As of PHP 5.4, it will be ignored an replaced by UTF-8.



技术细节

返回值: 返回转换后的字符串
PHP 版本: 4.3.0+
变更日志: PHP 5.6 - 更改了默认值字符集参数为默认字符集的值(在配置中)。
PHP 5.4 - 更改了默认值字符集参数为 UTF-8。
PHP 5.4 - 添加了 ENT_HTML401、ENT_HTML5、ENT_XML1 和 ENT_XHTML。
PHP 5.0 - 添加了对多字节编码的支持

更多示例

示例

将一些 HTML 实体转换为字符:

<?php
$str = "Albert Einstein said: &#039;E=MC&sup2;&#039;";
echo html_entity_decode($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo html_entity_decode($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo html_entity_decode($str, ENT_NOQUOTES); // Does not convert any quotes
?>

上述代码的 HTML 输出将是(查看源代码):

Albert Einstein said: &#039;E=MC²&#039;<br>
Albert Einstein said: 'E=MC²'<br>
Albert Einstein said: &#039;E=MC²&#039;

上述代码的浏览器输出将是:

Albert Einstein said: 'E=MC²'
Albert Einstein said: 'E=MC²'
Albert Einstein said: 'E=MC²'

示例

使用西欧字符集将一些 HTML 实体转换为字符:

<?php
$str = "My name is &Oslash;yvind &Aring;sane. I&#039;m Norwegian.";
echo html_entity_decode($str, ENT_QUOTES, "UTF-8");
?>

上述代码的 HTML 输出将是(查看源代码):

My name is Øyvind Åsane. I'm Norwegian.

上述代码的浏览器输出将是:

My name is Øyvind Åsane. I'm Norwegian.


❮ PHP 字符串参考