(greater than)"/> (greater than)">

Home  >  Article  >  Backend Development  >  PHP function htmlspecialchars_decode() that converts predefined html entities into characters

PHP function htmlspecialchars_decode() that converts predefined html entities into characters

黄舟
黄舟Original
2017-11-02 11:29:121807browse

实例

把预定义的 HTML 实体 "<"(小于)和 ">"(大于)转换为字符:

<?php
$str = "This is some &lt;b&gt;bold&lt;/b&gt; text.";
echo htmlspecialchars_decode($str);
?>

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

<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>

上面代码的浏览器输出如下:

This is some bold text.

定义和用法

htmlspecialchars_decode() 函数把一些预定义的 HTML 实体转换为字符。

  • 会被解码的 HTML 实体是:

  • & 解码成 & (和号)

  • " 解码成 " (双引号)

  • ' 解码成 ' (单引号)

  • < 解码成 cde6011ceacdff2371dbe328aea4c6fe (大于)

htmlspecialchars_decode() 函数是 htmlspecialchars() 函数的反函数。

语法

htmlspecialchars_decode(string,flags)
参数 描述
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 处理代码。

技术细节

返回值: 返回已转换的字符串。
PHP 版本: 5.1.0+
更新日志 在 PHP 5.4 中,新增了用于规定使用的文档类型的附加 flags:ENT_HTML401、ENT_HTML5、ENT_XML1 和 ENT_XHTML。

更多实例

实例 1

把一些预定义的 HTML 实体转换为字符:

<?php
$str = "Jane &amp; &#39;Tarzan&#39;";
echo htmlspecialchars_decode($str, ENT_COMPAT); //  默认,仅解码双引号echo "<br>";
echo htmlspecialchars_decode($str, ENT_QUOTES); //  解码双引号和单引号echo "<br>";
echo htmlspecialchars_decode($str, ENT_NOQUOTES); // 不解码任何引号
?>

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

<!DOCTYPE html>
<html>
<body>
Jane & &#39;Tarzan&#39;<br>Jane & &#39;Tarzan&#39;<br>Jane & &#39;Tarzan&#39;
</body>
</html>

上面代码的浏览器输出如下:

Jane & &#39;Tarzan&#39;
Jane & &#39;Tarzan&#39;
Jane & &#39;Tarzan&#39;

实例 2

把预定义 HTML 实体转换为双引号:

<?php
$str = &#39;I love &quot;PHP&quot;.&#39;;
echo htmlspecialchars_decode($str, ENT_QUOTES); // 解码双引号和单引号
?>

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

<!DOCTYPE html>
<html>
<body>I love "PHP".</body>
</html>

上面代码的浏览器输出如下:

I love "PHP".

htmlspecialchars_decode() 函数把一些预定义的 HTML 实体转换为字符。

<?php
$str = "This is some &lt;b&gt;bold&lt;/b&gt; text.";
echo htmlspecialchars_decode($str);
?>

会被解析成

<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>

用户商品详情的输出。

<p>
    {sh:$info.intro|htmlspecialchars_decode}
</p>

The above is the detailed content of PHP function htmlspecialchars_decode() that converts predefined html entities into characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn