htmlspecialchars_decode() – PHP String Functions
Syntax :
Description :
htmlspecialchars_decode() function will converts some predefined HTML entities back to characters. Here is the list of predefined characters. The converted entities are: & & (ampersand), " ” (double quote), (when ENT_NOQUOTES is not set), ' (when ENT_QUOTES is set), < and >.
It is opposite of htmlspecialchars() function.
Parameter :
- strings – This is Required parameter. String which needs to be converted.
- flags – This is an Optional parameter. The default value for flags is ENT_COMPAT | ENT_HTML401. It elaborates how to handle quotes and the used document type.
[table caption=”List of available flags constants are” max-width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Constant Name, Description
ENT_COMPAT, It will convert double-quotes only.
ENT_QUOTES, It will convert both double and single quotes.
ENT_NOQUOTES, It will not convert both double and single quotes.
ENT_HTML401, Handle code as HTML 4.01.
ENT_XML1, Handle code as XML 1.
ENT_XHTML, Handle code as XHTML.
ENT_HTML5, Handle code as HTML 5.
[/table]
Output :
This will return a decoded string.
ChangeLog :
[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
PHP 5.4.0 , The constants ENT_HTML401 / ENT_XML1 / ENT_XHTML and ENT_HTML5 were added.
[/table]
Related articles : htmlentities(), htmlspecialchars(), html_entity_decode(), get_html_translation_table().
htmlspecialchars_decode() – PHP Functions Example 1 :
<?php $strExample = '<a href="http://www.tutorialmines.net">Click to go tutorialmines</a><br />'; echo htmlspecialchars_decode($strExample); echo "<br />"; $strExample1 = '<b><i>www.tutorialmines.net. I am bold and italic.</i></b>'; echo htmlspecialchars_decode($strExample1); ?>
In above example ,We have a string ‘<a href=”http://www.tutorialmines.net”>Click to go tutorialmines</a>’; and string ‘<b><i>www.tutorialmines.net. I am bold and italic.</i></b>’;. Now see how the functions htmlspecialchars_decode() will convert them in the HTML view source of the page .
See below is the HTML output of above code view source of browser.
<!DOCTYPE html> <html> <body> <a href="http://www.tutorialmines.net">Click to go tutorialmines</a><br /><br /><b><i>www.tutorialmines.net. I am bold and italic.</i></b> </body> </html>
See below is the output of above code in web browser.
www.tutorialmines.net. I am bold and italic.
htmlspecialchars_decode() – PHP Functions Example 2 :
This example will show the use of different flags constants in htmlspecialchars_decode() function. We are taking single and double quotes in this example and see how this function behaves when constant flags are used –
<?php $str = ""He said, 'You can learn PHP here' ""; echo htmlspecialchars_decode($str, ENT_COMPAT); // This will only convert double quotes echo "<br/>"; echo htmlspecialchars_decode($str, ENT_QUOTES); // This will converts double and single quotes echo "<br/>"; echo htmlspecialchars_decode($str, ENT_NOQUOTES); // This will not convert any quotes ?>
See below is the HTML output of above code i.e. View Source in Web browser.
<!DOCTYPE html> <html> <body> "He said, 'You can learn PHP here' "<br/> "He said, 'You can learn PHP here' "<br/> "He said, 'You can learn PHP here' " </body> </html>
See below is the output of above code in Web browser.
“He said, ‘You can learn PHP here’ ”
“He said, ‘You can learn PHP here’ “
htmlspecialchars_decode() – PHP Functions Example 3 :
Convert some characters to HTML entities using the Western European character-set :
<?php $str = "Bonjour chers visiteurs ! <br /> N'hésitez pas à nous contacter pour tous vos projets ou questions concernant - <br /> Développement web <br /> Web Designing <br /> Services de référencement <br /> Google Classement de la page <br /> Développement d'applications Android."; echo htmlspecialchars_decode($str, ENT_QUOTES); // Will only convert double quotes (not single quotes), and uses the character-set Western European ?>
See below is the HTML output of above code i.e. View Source in Web browser.
<!DOCTYPE html> <html> <body> Bonjour chers visiteurs ! <br /> N'hésitez pas à nous contacter pour tous vos projets ou questions concernant - <br /> Développement web <br /> Web Designing <br /> Services de référencement <br /> Google Classement de la page <br /> Développement d'applications Android. </body> </html
See below is the output of above code in Web browser.