so what is the difference between htmlspecialchars() and htmlentities?


htmlspecialchars — Convert special characters to HTML entities


htmlentities — Convert all applicable characters to HTML entities



htmlentities

PHP CODE:
<?php
$str = "A 'quote' is <b>bold</b>";

echo htmlentities($str);
echo htmlentities($str, ENT_QUOTES);
?>

OUTPUT
A 'quote' is &lt;b&gt;bold&lt;/b&gt;

A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;


htmlspecialchars

PHP CODE:
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; //
?>

OUTPUT
&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;



for example, lets say there is a foreign character like the Spanish letter ñ - lets try it

Using htmlentities()
<?php echo htmlentities('ñ'); ?>

OUTPUT: &Atilde;&plusmn;


Using htmlspecialchars()

echo htmlspecialchars('ñ');
<?php echo htmlspecialchars('ñ'); ?>

OUTPUT: ñ

as you can see htmlentities() converts the ñ to the actual HTML code, where as htmlcharacters only coverts HTML tags for example