PHP htmlspecialchars_decode () function
Examples
The predefined HTML entities "& lt;" (less than) and "& gt;" (greater than) to a character:
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars_decode($str);
?>
HTML output of the code above is as follows (view source):
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>
Browser output of the code above is as follows:
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
参数 | 描述 |
---|---|
string | 必需。规定要解码的字符串。 |
flags | 可选。规定如何处理引号以及使用哪种文档类型。 可用的引号类型:
规定使用的文档类型的附加 flags:
|
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:
$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 output of the code above is as follows (view source):
<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'
Example 2
The predefined HTML entities into double quotes:
$str = 'I love "PHP".';
echo htmlspecialchars_decode($str, ENT_QUOTES); // Converts double and single quotes
?>
HTML output of the code above is as follows (view source):
<html>
<body>
I love "PHP".
</body>
</html>
Browser output of the code above is as follows:
PHP String Reference