htmlspecialchars() function can converts some predefined characters to HTML entities. Here is list of predefined characters.
[table caption=”Predefined Characters List” max-width=”100%” colwidth=”25%|75%” colalign=”left|left”]
[/table]
Tip: To convert special HTML entities back to characters, use the htmlspecialchars_decode() function.
[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_IGNORE, Silently discard invalid code unit sequences instead of returning an empty string. Should not be used to avoid security implications.
ENT_SUBSTITUTE, Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty string.
ENT_DISALLOWED, Replace invalid code points for the given document type with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of leaving them as is.
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]
string
itself is valid for the encoding, as the characters affected by htmlspecialchars() occupy the same positions in all of these encodings.This will return a converted string.
[table caption=”ChangeLog ” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
5.6.0, The default value for the encoding parameter was changed to be the value of the default_charset configuration option.
5.4.0, The default value for the encoding parameter was changed to UTF-8.
5.3.0, The constant ENT_IGNORE was added.
5.2.3, The double_encode parameter was added.
[/table]
<?php $strExample = '<a href="http://www.tutorialmines.net">Click to go tutorialmines</a>'; echo htmlspecialchars($strExample); echo "<br />"; $strExample1 = '<b><i>www.tutorialmines.net. I am bold and italic.</i></b>'; echo htmlspecialchars($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() 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 /> <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 :
This example will show the use of different flags constants in htmlspecialchars() 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($str, ENT_COMPAT); // This will only convert double quotes echo "<br/>"; echo htmlspecialchars($str, ENT_QUOTES); // This will converts double and single quotes echo "<br/>"; echo htmlspecialchars($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.
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($str, ENT_QUOTES, "UTF-8"); // 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.
Q – What are the differences between htmlspecialchars() and htmlentities(). When should I use one or the other? When to use htmlspecialchars() or htmlentities()?
A – htmlspecialchars () does the minimum amount of encoding, which ensure that our string is not parsed as HTML. Which results in that our string is more human-readable than it would be if you used htmlentities () to encode absolutely everything that has an encoding.
When there is no need to encode all characters which have their HTML equivalents. use htmlspecialchars ().
htmlspecialchars is much straightforward, and produce less code to send to the client.
When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…
In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…
In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…
In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…
When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…
In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…