PHP htmlspecialchars() Function

The htmlspecialchars() function converts some predefined characters (<, >, single quote ‘, double quote " and ampersand) to HTML entities.


$string – Required. Specifies the string to convert

$quotestyle – Optional. Specifies how to encode single and double quotes.
The available quote styles are:
– ENT_COMPAT – Default. Encodes only double quotes
– ENT_QUOTES – Encodes double and single quotes
– ENT_NOQUOTES – Does not encode any quotes

See below example,

$string = "Me & 'You'";
echo htmlspecialchars($string, ENT_COMPAT);
echo "<br />";
echo htmlspecialchars($string, ENT_QUOTES);
echo "<br />";
echo htmlspecialchars($string, ENT_NOQUOTES);

Output of the code above will be:

Me & ‘You’
Me & ‘You’
Me & ‘You’

Select “View source” in the browser window, you will see the following HTML:

Me &amp; 'You'<br />Me &amp; &#039;You&#039;<br />Me &amp; 'You'

You may refer this application Encoding HTML Tags for encoding html tags – for putting HTML code in the blogger ( blogs.

Posted in Php