Quantcast
Channel: Milap Patel » String Functions
Viewing all articles
Browse latest Browse all 5

PHP htmlentities() – convert characters to html entities

$
0
0

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 >.


Filed under: htmlentities, String Functions

Viewing all articles
Browse latest Browse all 5

Trending Articles