PHP htmlentities() – convert characters to html entities

PHP htmlentities function is used for covert the characters to its HTML entities. This function will automatically identify the characters which have an equivalent HTML entity , and then convert to it.

Its Synatx will be ;
htmlentities ( string , quote style , Character set );

First parametrer will be the string to convert
Second parameter is optional. Here we can specify how to deal with single quotes (‘) and double quotes (“)

Available quote styles are

  • ENT_COMPAT : Will convert double-quotes and leave single-quotes alone.
  • ENT_QUOTES : Will convert both double and single quotes.
  • ENT_NOQUOTES : Will leave both double and single quotes unconverted.

Third parameter is also optional , which defines the character set used in conversion. By default character set will be ISO-8859-1.

Example :-

<?php
$data="This is milap's good example of <b>htmlentities<b>";
echo htmlentities($data);
echo "<br>";
echo htmlentities($data,ENT_QUOTES)
?>

Output :-

This is milap’s good example of <b>htmlentities<b>
This is milap’s good example of <b>htmlentities<b>

View Source :-
This is milap’s good example of &lt;b&gt;htmlentities&lt;b&gt;
<br>This is milap’s good example of &lt;b&gt;htmlentities&lt;b&gt;

first echo statement will return ‘ as it is because second argument is not given,
but second echo statement will return ‘ as a html code for ‘ after milap.
We can see that &lt;is related to <.and  &gt; is related to >.

Advertisements

I like to hear from you about this !!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s