PHP htmlspecialchars_decode() Function
PHP htmlspecialchars_decode() Function
The htmlspecialchars_decode() function in PHP convert some predefined HTML entities to characters.
The some predefined HTML entities that will be decoded are as follows:
- & OR & (ampersand)
- " OR " (double quote)
- ' OR ' (single quote)
- < OR < (less than)
- > OR > (greater than)
Syntax
htmlspecialchars_decode(string,flags)
Parameter
string(required)- This parameter specifies the string to decode.
flags(optional)- This parameter specifies how to handle quotes, invalid encoding and the used document type. The available flag constants are as follows:
- ENT_COMPAT- Table contains entities only for double-quotes
- ENT_QUOTES- The Table contains entities for both double and single quotes
- ENT_NOQUOTES- Table contains entities neither for single quotes nor for double quotes.
- ENT_HTML401- Table specifically for HTML 4.01.
- ENT_XML1- Table for XML 1.
- ENT_XHTML- Table for XHTML.
- ENT_HTML5- Table for HTML 5.
Return
This function returns the decoded string.
Example 1
<?php $str = "My name is &Amar. I'm from 'India'.."; // using the Western European character-set: echo htmlspecialchars_decode($str, ENT_QUOTES);// Will only both single as well as double quotes ?>
Output
My name is &Amar. I'm from 'India'..
Example 2
<?php // initializing the string with special characters $str = "<'Tutorial>&<Example'>"; echo htmlspecialchars_decode($str, ENT_COMPAT); // Will only convert double quotes echo "\n"; echo htmlspecialchars_decode($str, ENT_QUOTES); // will convert the double and single quotes echo "\n"; echo htmlspecialchars_decode($str, ENT_NOQUOTES); // Does not convert any quotes ?>
Output
<'Tutorial>&<Example'> <'Tutorial>&<Example'> <'Tutorial>&<Example'>
Example 3
<?php $str = '"PHP" is easy to learn.'; echo htmlspecialchars_decode($str, ENT_QUOTES); // Converting the double and single quotes ?>
Output
"PHP" is easy to learn.
