htmlentities – PHP String Functions

Syntax :

htmlentities ( strings, flags, encoding/character-set, double_encode );

Description :

htmlentities() function will converts all possible characters to HTML entities.

Note : html_entity_decode() function is reverse of it. You can convert HTML entities back to characters with its help.
Note : This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

Parameter :

  • strings – This is Required parameter. String which needs to be converted.
  • flags – This is an Optional parameter. The default value for flags is ENT_COMPAT | ENT_HTML401. It elaborates how to handle quotes, invalid encoding and the used document type.

[table caption=”List of available flags constants are” max-width=”100%” colwidth=”25%|75%” colalign=”left|left”]

Constant Name, Description
ENT_COMPAT, It will convert double-quotes only.
ENT_QUOTES, It will convert both double and single quotes.
ENT_NOQUOTES, It will not convert both double and single quotes.
ENT_IGNORE, Silently discard invalid code unit sequences instead of returning an empty string. Should not be used to avoid security implications.
ENT_SUBSTITUTE, Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty string.
ENT_DISALLOWED, Replace invalid code points for the given document type with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of leaving them as is.
ENT_HTML401, Handle code as HTML 4.01.
ENT_XML1, Handle code as XML 1.
ENT_XHTML, Handle code as XHTML.
ENT_HTML5, Handle code as HTML 5.
[/table]

  • encoding – This is an Optional parameter. It defines which encoding is to be used when converting characters.
    • If omitted, the default value of the encoding varies depending on the PHP version in use. In PHP 5.6 and later, the default_charset configuration option is used as the default value. PHP 5.4 and 5.5 will use UTF-8 as the default. Earlier versions of PHP use ISO-8859-1.Although this argument is technically optional, you are highly encouraged to specify the correct value for your code if you are using PHP 5.5 or earlier, or if your default_charset configuration option may be set incorrectly for the given input.The following character sets are supported:
    • Supported charsets
      [table caption=”List of Supported charsets are” max-width=”100%” colwidth=”25%|75%” colalign=”left|left”]
      Charset Aliases, Description
      ISO-8859-1,Western European Latin-1.
      ISO-8859-5 , Little used cyrillic charset (Latin/Cyrillic).
      ISO-8859-15 , Western European,
      UTF-8, ASCII compatible multi-byte 8-bit Unicode.
      cp866 ibm866, DOS-specific Cyrillic charset.
      cp1251 , Windows-specific Cyrillic charset.
      cp1252 , Windows specific charset for Western European.
      KOI8-R,  Russian.
      BIG5 950, Traditional Chinese. mainly used in Taiwan.
      GB2312 936, Simplified Chinese. national standard character set.
      BIG5-HKSCS , with Hong Kong extensions. Traditional Chinese.
      Shift_JIS , Japanese
      EUC-JP , Japanese
      MacRoman Charset, that was used by Mac OS.
      [/table]
      ” An empty string activates detection from script encoding (Zend multibyte), default_charset and current locale (see nl_langinfo() and setlocale()), in this order. Not recommended.
      Note: Any other character sets are not recognized. The default encoding will be used instead and a warning will be emitted.
  • double_encode – When double_encode is turned off PHP will not encode existing html entities. The default is to convert everything.Optional. A boolean value that specifies whether to encode existing html entities or not.
    TRUE – Default. Will convert everything
    FALSE – Will not encode existing html entities

Output :

This will return a converted string.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
PHP 5.4.0 , The default value for the encoding parameter was changed to UTF-8.
PHP 5.4.0 ,The constants ENT_HTML401 / ENT_XML1 / ENT_XHTML / ENT_HTML5 were introduced.
PHP 5.3.4 , The encoding parameter was added.
[/table]


Related articles :  get_html_translation_table()htmlspecialchars(), html_entity_decode().


htmlentities() – PHP Functions Example 1 :
<?php
$strExample = '<a href="http://www.tutorialmines.net">Click to go tutorialmines</a>';
echo htmlentities($strExample);
echo "<br />";
$strExample1 = '<b><i>www.tutorialmines.net. I am bold and italic.</i></b>';
echo htmlentities($strExample1);
?>

In above example ,We have a string ‘<a href=”http://www.tutorialmines.net”>Click to go tutorialmines</a>’; and string ‘<b><i>www.tutorialmines.net. I am bold and italic.</i></b>’;. Now see how the functions htmlentities() will convert them in the HTML view source of the page .
See below is the HTML output of above code view source of browser.

<!DOCTYPE html>
<html>
<body>

&lt;a href=&quot;http://www.tutorialmines.net&quot;&gt;Click to go tutorialmines.net&lt;/a&gt; 
<br />&lt;b&gt;&lt;i&gt;www.tutorialmines.net. I am bold and italic.&lt;/i&gt;&lt;/b&gt;
</body>
</html>

See below is the output of above code in web browser.

<a href=”http://www.tutorialmines.net”>Click to go tutorialmines.net</a>
<b><i>www.tutorialmines.net. I am bold and italic.</i></b>

htmlentities() – PHP Functions Example 2 :

This example will show the use of different flags constants in htmlentities() function. We are taking single and double quotes in this example and see how this function behaves when constant flags are used –

<?php
$str = "We're here to help you to learn PHP. \" using double quotes also.";
echo htmlentities($str, ENT_COMPAT); // This will only convert double quotes
echo "<br/>";
echo htmlentities($str, ENT_QUOTES); // This will converts double and single quotes
echo "<br/>";
echo htmlentities($str, ENT_NOQUOTES); // This will not convert any quotes
?> 

See below is the HTML output of above code i.e. View Source in Web browser.

<!DOCTYPE html>
<html>
<body>
We're here to help you to learn PHP. &quot; using double quotes also.<br/>We&#039;re here to help you to learn PHP. &quot; using double quotes also.<br/>We're here to help you to learn PHP. " using double quotes also. 
</body>
</html>

See below is the output of above code in Web browser.

We’re here to help you to learn PHP. ” using double quotes also.
We’re here to help you to learn PHP. ” using double quotes also.
We’re here to help you to learn PHP. ” using double quotes also.

htmlentities() – PHP Functions Example 3 :

Convert some characters to HTML entities using the Western European character-set :

<?php
$str = "Bonjour chers visiteurs ! <br />
N'hésitez pas à nous contacter pour tous vos projets ou questions concernant - <br />
Développement web <br />
Web Designing <br />
Services de référencement <br />
Google Classement de la page <br />
Développement d'applications Android.";
echo htmlentities($str, ENT_QUOTES, "UTF-8"); // Will only convert double quotes (not single quotes), and uses the character-set Western European
?>

See below is the HTML output of above code i.e. View Source in Web browser.

<!DOCTYPE html>
<html>
<body>
Bonjour chers visiteurs ! &lt;br /&gt;
N&#039;h&eacute;sitez pas &agrave; nous contacter pour tous vos projets ou questions concernant - &lt;br /&gt;
D&eacute;veloppement web &lt;br /&gt;
Web Designing &lt;br /&gt;
Services de r&eacute;f&eacute;rencement &lt;br /&gt;
Google Classement de la page &lt;br /&gt;
D&eacute;veloppement d&#039;applications Android.

</body>
</html>

See below is the output of above code in Web browser.

Bonjour chers visiteurs ! <br /> N’hésitez pas à nous contacter pour tous vos projets ou questions concernant – <br /> Développement web <br /> Web Designing <br /> Services de référencement <br /> Google Classement de la page <br /> Développement d’applications Android.

Q – What are the differences between htmlspecialchars() and htmlentities(). When should I use one or the other?When to use htmlspecialchars() or htmlentities()?

A – htmlspecialchars () does the minimum amount of encoding, which ensure that our string is not parsed as HTML. Which results in that our string is more human-readable than it would be if you used htmlentities () to encode absolutely everything that has an encoding.

When there is no need to encode all characters which have their HTML equivalents. use htmlspecialchars ().

htmlspecialchars is much straightforward, and produce less code to send to the client.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *