- Forums
- PHP
- PHP Htmlentities Vs Htmlspecialchars Differences Php Functions
this page will explain what is the major difference in using htmlentities versus using htmlspecialchars functions in php we are going to compare how to use each [1093], Last Updated: Sat May 18, 2024
wallpaperama
Thu Feb 21, 2008
5 Comments
6467 Visits
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 <b>bold</b>
A 'quote' is <b>bold</b>
htmlspecialchars
PHP CODE:
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; //
?>
OUTPUT
<a href='test'>Test</a>
for example, lets say there is a foreign character like the Spanish letter ñ - lets try it
Using htmlentities()<?php echo htmlentities('ñ'); ?>
OUTPUT: ñ
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