PHP htmlspecialchars_decode()
Function
Example
Convert predefined HTML entities "<" (less than) and ">" (greater than) to characters:
<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars_decode($str);
?>
The HTML output of the above code is as follows (view source):
<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>
The browser output of the above code is as follows:
This is some bold text.
Definition and Usage
The htmlspecialchars_decode()
function converts some predefined HTML entities to characters.
The HTML entities that will be decoded are:
&
decodes to&
(ampersand)"
decodes to"
(double quote)'
decodes to'
(single quote)<
decodes to<
(less than)>
decodes to>
(greater than)
The htmlspecialchars_decode()
function is the inverse of the htmlspecialchars()
function.
Syntax
Parameter | Description |
---|---|
string | Required. Specifies the string to decode. |
flags | Optional. Specifies how to handle quotes and which document type to use. Available quote types: ENT_COMPAT - Default. Decodes double quotes only.<br> ENT_QUOTES - Decodes both double and single quotes.<br> ENT_NOQUOTES - Does not decode any quotes. Additional flags for specifying the document type: ENT_HTML401 - Default. Handles code as HTML 4.01.<br> ENT_HTML5 - Handles code as HTML 5.<br> ENT_XML1 - Handles code as XML 1.<br> ENT_XHTML - Handles code as XHTML. |
Technical Details
Return Value: | Returns the converted string. |
---|---|
PHP Version: | 5.1.0+ |
--- | --- |
Changelog: | Added additional flags for specifying the document type in PHP 5.4: ENT_HTML401, ENT_HTML5, ENT_XML1, and ENT_XHTML. |
--- | --- |
More Examples
Example 1
Convert some predefined HTML entities to characters:
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars_decode($str, ENT_COMPAT); // Default, decodes double quotes only
echo "<br>";
echo htmlspecialchars_decode($str, ENT_QUOTES); // Decodes both double and single quotes
echo "<br>";
echo htmlspecialchars_decode($str, ENT_NOQUOTES); // Does not decode any quotes
?>
The HTML output of the above code is as follows (view source):
The browser output of the above code is as follows:
Jane & 'Tarzan'
Jane & 'Tarzan'
Jane & 'Tarzan'
Example 2
Convert predefined HTML entities to double quotes:
<?php
$str = 'I love "PHP".';
echo htmlspecialchars_decode($str, ENT_QUOTES); // Decodes both double and single quotes
?>
The HTML output of the above code is as follows (view source):
<!DOCTYPE html>
<html>
<body>
I love "PHP".
</body>
</html>
The browser output of the above code is as follows:
I love "PHP".