PHP htmlspecialchars_decode() Function
Definition
The htmlspecialchars_decode() function converts some predefined HTML entities to characters.
HTML entities that will be decoded are:
- & becomes & (ampersand)
- " becomes " (double quote)
- ' becomes ' (single quote)
- < becomes < (less than)
- > becomes > (greater than)
The htmlspecialchars_decode() function is the opposite of htmlspecialchars().
Syntax
PHP htmlspecialchars_decode() Function has the following syntax.
htmlspecialchars_decode(string,flags)
Parameter
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to decode |
flags | Optional. | How to handle quotes and which document type to use. |
The available quote styles are:
- ENT_COMPAT - Default. Decodes only double quotes
- ENT_QUOTES - Decodes double and single quotes
- ENT_NOQUOTES - Does not decode any quotes
- ENT_HTML401 - Default. Handle code as HTML 4.01
- ENT_HTML5 - Handle code as HTML 5
- ENT_XML1 - Handle code as XML 1
- ENT_XHTML - Handle code as XHTML
Return
PHP htmlspecialchars_decode() Function returns the converted string.
Example 1
Convert the predefined HTML entities "<" (less than) and ">" (greater than) to characters:
<?php
$str = "java2s.com <b>bold</b> text.";
echo htmlspecialchars_decode($str);
?>
The code above generates the following result.
Example 2
Convert some predefined HTML entities to characters:
<?php/* w ww .j a v a 2 s . co m*/
$str = "Java & '.com'";
echo htmlspecialchars_decode($str, ENT_COMPAT); // Will only convert double quotes
echo "\n";
echo htmlspecialchars_decode($str, ENT_QUOTES); // Converts double and single quotes
echo "\n";
echo htmlspecialchars_decode($str, ENT_NOQUOTES); // Does not convert any quotes
?>
The code above generates the following result.