Latest web development tutorials

PHP htmlspecialchars_decode () function

PHP String Reference PHP String Reference

Examples

The predefined HTML entities "& lt;" (less than) and "& gt;" (greater than) to a character:

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

HTML output of the code above is as follows (view source):

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

Browser output of the code above is as follows:

This is some bold text.


Definition and Usage

htmlspecialchars_decode () function to some predefined HTML entities to characters.

Will be decoded HTML entities are:

  • & Amp; decoded into & (ampersand)
  • & Quot; decoded into "(double quotation marks)
  • 'Decoded into a' (single quotes)
  • & Lt; decoded as <(less than)
  • & Gt; decoded to> (greater than)

htmlspecialchars_decode () function is htmlspecialchars () the inverse function of the function.


grammar

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 处理代码。

technical details

return value: Returns the converted string.
PHP version: 5.1.0+
Update log: In PHP 5.4, add the requirement to use additional flags for the document type: ENT_HTML401, ENT_HTML5, ENT_XML1 and ENT_XHTML.


More examples

Example 1

Some predefined HTML entities into characters:

<?php
$str = "Jane &amp; '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 output of the code above is as follows (view source):

<!DOCTYPE html>
<html>
<body>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'
</body>
</html>

Browser output of the code above is as follows:

Jane & 'Tarzan'
Jane & 'Tarzan'
Jane & 'Tarzan'


Example 2

The predefined HTML entities into double quotes:

<?php
$str = 'I love &quot;PHP&quot;.';
echo htmlspecialchars_decode($str, ENT_QUOTES); // Converts double and single quotes
?>

HTML output of the code above is as follows (view source):

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

Browser output of the code above is as follows:

I love "PHP".



PHP String Reference PHP String Reference