函数名:html_entity_decode()
适用版本:PHP 4, PHP 5, PHP 7
用法:html_entity_decode() 函数将 HTML 实体转换为对应的字符。它可以处理所有被 htmlentities() 或 htmlspecialchars() 转换为实体的字符。
语法:html_entity_decode(string $string, int $flags = ENT_COMPAT | ENT_HTML401, string|null $encoding = ini_get("default_charset")): string|false
参数:
- $string:必需,要进行实体转换的字符串。
- $flags:可选,指定如何处理引号和特殊字符的标志。默认为 ENT_COMPAT | ENT_HTML401,表示仅转换双引号,并且使用 HTML 4.01 的字符实体。
- $encoding:可选,指定要使用的字符编码。如果未指定,则使用 ini_get("default_charset") 的默认值。
返回值:返回转换后的字符串,如果出错则返回 false。
示例:
$html = "<p>This is a <b>sample</b> text.</p>";
$decodedHtml = html_entity_decode($html);
echo $decodedHtml;
// 输出:"<p>This is a <b>sample</b> text.</p>"
在上述示例中,我们将包含 HTML 实体的字符串 $html
使用 html_entity_decode()
函数进行解码,并将解码后的结果赋值给变量 $decodedHtml
。然后,我们使用 echo
输出解码后的字符串,即实体被转换为相应的字符。