定义和用法
html_entity_decode() 函数把 HTML 实体转换为字符。
html_entity_decode() 函数是 htmlentities() 函数的反函数。
语法
html_entity_decode(string,flags,character-set)
| 参数 | 描述 |
|---|---|
| string | 必需。规定要解码的字符串。 |
| flags |
可选。规定如何处理引号以及使用哪种文档类型。 可用的引号类型:
规定所使用文档类型的附加 flags:
|
| character-set |
可选。字符串值,规定要使用的字符集。 允许的值:
注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。 |
技术细节
| 返回值: | 返回被转换的字符串 |
| PHP 版本: | 4.3.0+ |
更新日志:
| 版本 | 说明 |
|---|---|
| PHP 5 | character-set 参数的默认值改为 UTF-8。 |
| PHP 5.4 |
新增了用于规定翻译表适用的文档类型的附加 flags:
|
| 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. 继续阅读与本文标签相同的文章
-
乌镇“互联网之光”博览会上的5G元素
2026-05-14栏目: 教程
-
华为高管彭博:正与美国公司就授权5G平台展开初期谈判
2026-05-14栏目: 教程
-
微信曝光新功能,超好用,再也不用担心被刷屏
2026-05-14栏目: 教程
-
Verizon为美国多座大型体育场馆提供了5G网络覆盖
2026-05-14栏目: 教程
-
别人加薪你加班,偷偷告诉你 6 个Word小技巧,比加薪都管用!
2026-05-14栏目: 教程
