PHP html_entity_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 code):
<!DOCTYPE html> <html> <body> This is some <b>bold</b> text. </body> </html>
Browser output of the above code:
This is some bold text.
Definition and Usage
The htmlspecialchars_decode() function converts predefined HTML entities to characters.
The HTML entities that will be decoded are:
- & Decoded to & (ampersand)
- " Decoded to " (double quote)
- ' Decoded to ' (single quote)
- < Decoded to < (less than)
- > Decoded to > (greater than)
The htmlspecialchars_decode() function is the inverse of the htmlspecialchars() function.
Syntax
htmlspecialchars_decode(string,flags)
Parameter | Description |
---|---|
string | Required. Specifies the string to be decoded. |
flags |
Optional. Specifies how quotes are handled and which document type is used. Available quote types:
Additional flags for the document type used:
|
Technical Details
Return Value: | Return the converted string. |
PHP Version: | 5.1.0+ |
Update Log: |
In PHP 5.4, additional flags were added to specify the document type to be used:
|
More Examples
Example 1
Convert predefined HTML entities to characters:
<?php $str = "Bill & 'Steve'"; echo htmlspecialchars_decode($str, ENT_COMPAT); // Only convert double quotes echo "<br>"; echo htmlspecialchars_decode($str, ENT_QUOTES); // Convert double quotes and single quotes echo "<br>"; echo htmlspecialchars_decode($str, ENT_NOQUOTES); // Do not convert any quotes ?>
The HTML output of the above code is as follows (view source code):
<!DOCTYPE html> <html> <body> Bill & 'Steve'<br> Bill & 'Steve'<br> Bill & 'Steve' </body> </html>
Browser output of the above code:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
Example 2
Convert predefined HTML entities to double quotes:
<?php $str = 'I love "PHP".'; echo htmlspecialchars_decode($str, ENT_QUOTES); // Convert double quotes and single quotes ?>
The HTML output of the above code is as follows (view source code):
<!DOCTYPE html> <html> <body> I love "PHP". </body> </html>
Browser output of the above code:
I love "PHP".