Home > DeveloperSection > Interviews > What’s the difference between htmlentities() and htmlspecialchars()?

Posted on    March-18-2011 12:00 AM

 PHP PHP 
Ratings:
 1 Answer(s)
  922  View(s)
Rate this:

Sanjay Sharma
Sanjay Sharma

Total Post:42

Points:210
Posted on    March-18-2011 12:00 AM


Difference between htmlentities() and htmlspecialchars()

htmlentities

PHP CODE:
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;

Modified On Apr-04-2016 01:21:28 AM

Don't want to miss updates? Please click the below button!

Follow MindStick