PHP htmlspecialchars_decode() Function
Topic: PHP String ReferencePrev|Next
Description
The htmlspecialchars_decode()
function converts special HTML entities back to their corresponding characters. The special HTML entities are:
&
converted to&
(ampersand)"
converted to"
(double quote), whenENT_NOQUOTES
is not set.'
converted to'
(single quote), whenENT_QUOTES
is set.<
converted to<
(less than)>
converted to>
(greater than)
This function typically reverses the effect of htmlspecialchars()
function.
The following table summarizes the technical details of this function.
Return Value: | Returns the decoded string. |
---|---|
Version: | PHP 5.1.0+ |
Syntax
The basic syntax of the htmlspecialchars_decode()
function is given with:
The following example shows the htmlspecialchars_decode()
function in action.
Example
Run this code »<?php
// Sample string
$str = "The lions & tigers live in <b>dense</b> forest.";
// Encoding the string
$encoded_str = htmlspecialchars($str);
echo $encoded_str . "<br>";
// Decoding the string
$decoded_str = htmlspecialchars_decode($encoded_str);
echo $decoded_str;
?>
Parameters
The htmlspecialchars_decode()
function accepts the following parameters.
Parameter | Description |
---|---|
string | Required. Specifies the string to decode. |
flags |
Optional. Specifies how to handle quotes and which document type to use. The available flags constants for handling quotes are:
The available flags constants for specifying the document types are:
The default value for this parameter is |
More Examples
Here're some more examples showing how htmlspecialchars_decode()
function actually works:
The following example demonstrates the handling of single and double quotes using this function.
Example
Run this code »<?php
// Sample string
$str = "I'll \"leave\" tomorrow.";
// Encoding the string
$encoded_str = htmlspecialchars($str, ENT_QUOTES);
echo $encoded_str; /* I'll "leave" tomorrow. */
// Decodes only double-quotes
$a = htmlspecialchars_decode($encoded_str);
echo $a; /* I'll "leave" tomorrow. */
// Decodes both double and single quotes
$b = htmlspecialchars_decode($encoded_str, ENT_QUOTES);
echo $b; /* I'll "leave" tomorrow. */
?>
However, in the browser you will always see the string I'll "leave" tomorrow.
View source (right-click and select View Page Source) of the example output to see the converted string.