PHP htmlspecialchars_decode() 函数
实例
把预定义的 HTML 实体 "<"(小于)和 ">"(大于)转换为字符:
 <?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars_decode($str);
?>
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars_decode($str);
?>
以上代码的 HTML 输出如下(查看源代码):
 <!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>
以上代码的浏览器输出:
This is some bold text.
定义和用法
htmlspecialchars_decode() 函数把预定义的 HTML 实体转换为字符。
会被解码的 HTML 实体是:
- & 解码成 & (和号)
 - " 解码成 " (双引号)
 - ' 解码成 ' (单引号)
 - < 解码成 < (小于)
 - > 解码成 > (大于)
 
htmlspecialchars_decode() 函数是 htmlspecialchars() 函数的反函数。
语法
htmlspecialchars_decode(string,flags)
参数值
| 参数 | 描述 | 
|---|---|
| string | 必需。规定要解码的字符串。 | 
| flags | 
 可选。规定如何处理引号以及使用哪种文档类型。 可用的引号类型: 
 规定使用的文档类型的附加 flags: 
  | 
技术细节
| 返回值: | 返回已转换的字符串。 | 
|---|---|
| PHP 版本: | 5.1.0+ | 
| 更新日志: | 
 在 PHP 5.4 中,新增了用于规定使用的文档类型的附加 flags: 
  | 
更多实例
实例
把预定义的 HTML 实体转换为字符:
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars_decode($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlspecialchars_decode($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlspecialchars_decode($str, ENT_NOQUOTES); // Does not convert any quotes
?>
$str = "Jane & 'Tarzan'";
echo htmlspecialchars_decode($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlspecialchars_decode($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlspecialchars_decode($str, ENT_NOQUOTES); // Does not convert any quotes
?>
以上代码的 HTML 输出如下(查看源代码):
 <!DOCTYPE html>
<html>
<body>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'
</body>
</html>
<html>
<body>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'
</body>
</html>
以上代码的浏览器输出:
Jane & 'Tarzan'
Jane & 'Tarzan'
Jane & 'Tarzan'
Jane & 'Tarzan'
Jane & 'Tarzan'
实例
把预定义 HTML 实体转换为双引号:
 <?php
$str = 'I love "PHP".';
echo htmlspecialchars_decode($str, ENT_QUOTES); // Converts double and single quotes
?>
$str = 'I love "PHP".';
echo htmlspecialchars_decode($str, ENT_QUOTES); // Converts double and single quotes
?>
以上代码的 HTML 输出如下(查看源代码):
 <!DOCTYPE html>
<html>
<body>
I love "PHP".
</body>
</html>
<html>
<body>
I love "PHP".
</body>
</html>
以上代码的浏览器输出:
I love "PHP".
