Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

htmlentities() vs htmlspecialchars() Function

 What is the difference between htmlentities() and htmlspecialchars() in PHP


htmlentities() Function

htmlentities — Convert all applicable characters to HTML entities.
The htmlentities() function is an inbuilt function in PHP which is used to transform all characters which are applicable to HTML entities. This function converts all characters that are applicable to HTML entity.

Syntax
htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get(“default_charset”) [, bool $double_encode = TRUE ]]] ) : string

Example #1 htmlentities()

<?php
// String convertable to htmlentities
$str = ‘<a href=”http://www.nikunjjoshiphpdeveloper.com”>Nikunj Joshi PHP Developer</a>’;

// It will convert htmlentities and print them
echo htmlentities( $str );
?>

Example #2 htmlentities()

<?php
$str = “A ‘quote’ is <b>bold</b>”;

// Outputs: A ‘quote’ is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);

// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str, ENT_QUOTES);
?>

htmlspecialchars() Function

The htmlspecialchars() function is an inbuilt function in PHP which is used to convert all predefined characters to HTML entities.
The htmlspecialchars() function converts some predefined characters to HTML entities.

Example #1
<?php
$str = “This is some <b>bold</b> text.”;
echo htmlspecialchars($str);
?>

The browser output of the code above will be:

This is some bold text.


Post a Comment

0 Comments