当前位置: 首页> 函数类别大全> html_entity_decode

html_entity_decode

将HTML实体转换为相应的字符
名称:html_entity_decode
分类:字符串
所属语言:php
一句话介绍:把 HTML 实体转换为字符。

定义和用法

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

html_entity_decode() 函数是 htmlentities() 函数的反函数。

实例

例子 1

把 HTML 实体转换为字符:

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

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

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

以上代码的浏览器输出:

<? W3S?h????>

例子 2

把 HTML 实体转换为字符:

<?php
$str = "Bill &amp; &#039;Steve&#039;";
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 & &#039;Steve&#039;<br>
Bill & 'Steve'<br>
Bill & &#039;Steve&#039;
</body>
</html>

以上代码的浏览器输出:

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

例子 3

通过使用西欧字符集,把 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.

语法

html_entity_decode(string,flags,character-set)
参数 描述
string 必需。规定要解码的字符串。
flags

可选。规定如何处理引号以及使用哪种文档类型。

可用的引号类型:

  • ENT_COMPAT - 默认。仅解码双引号。
  • ENT_QUOTES - 解码双引号和单引号。
  • ENT_NOQUOTES - 不解码任何引号。

规定所使用文档类型的附加 flags:

  • ENT_HTML401 - 默认。作为 HTML 4.01 处理代码。
  • ENT_HTML5 - 作为 HTML 5 处理代码。
  • ENT_XML1 - 作为 XML 1 处理代码。
  • ENT_XHTML - 作为 XHTML 处理代码。
character-set

可选。字符串值,规定要使用的字符集。

允许的值:

  • UTF-8 - 默认。ASCII 兼容多字节的 8 位 Unicode
  • ISO-8859-1 - 西欧
  • ISO-8859-15 - 西欧(加入欧元符号 + ISO-8859-1 中丢失的法语和芬兰语字母)
  • cp866 - DOS 专用 Cyrillic 字符集
  • cp1251 - Windows 专用 Cyrillic 字符集
  • cp1252 - Windows 专用西欧字符集
  • KOI8-R - 俄语
  • BIG5 - 繁体中文,主要在台湾使用
  • GB2312 - 简体中文,国家标准字符集
  • BIG5-HKSCS - 带香港扩展的 Big5
  • Shift_JIS - 日语
  • EUC-JP - 日语
  • MacRoman - Mac 操作系统使用的字符集

注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。

同类函数
  • 使用一个字符串分割另一个字符串为数组 explode

    explode

    使用一个字符串分割另一个字符串为数组
  • 二进制安全字符串比较 strcmp

    strcmp

    二进制安全字符串比较
  • 将字符串中每个单词的首字母转换为大写 ucwords

    ucwords

    将字符串中每个单词的首字母转换为大写
  • 将8位字符串转换为带引号的可打印字符串 quoted_printable_encode

    quoted_printable_encode

    将8位字符串转换为带引号的可打印字符串
  • 将格式化后的字符串写入到流 fprintf

    fprintf

    将格式化后的字符串写入到流
  • rtrim的别名 chop

    chop

    rtrim的别名
  • 将HTML实体转换为相应的字符 html_entity_decode

    html_entity_decode

    将HTML实体转换为相应的字符
  • 将字符串解析成多个变量 parse_str

    parse_str

    将字符串解析成多个变量
热门文章