html_entity_decode() – PHP String Functions

Syntax :

html_entity_decode ( strings, flags, encoding/character-set );

Description :

html_entity_decode() function will converts HTML entities to their applicable characters.

Note : htmlentities() function is reverse of it.

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 decodes double-quotes only.
ENT_QUOTES, It will decodes both double and single quotes.
ENT_NOQUOTES, It will not decodes both double and single quotes.
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/character-set – This is an Optional parameter. A string that specifies which character-set to use.
    • 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 are :
      [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.
      Empty string or ”, An empty string activates detection from script encoding (Zend multibyte) or default_charset and current locale (see nl_langinfo() and setlocale()) so in this order. Not recommended.[/table]

Note : Any other character sets are not recognized. The default encoding will be used instead and a warning will be emitted.


Output :

This will return a decoded string.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
PHP 5.6.0, The default value for the encoding parameter was changed to be the value in the default_charset configuration option.
PHP 5.4.0 , The default value for the encoding parameter was changed ISO-8859-1 to UTF-8.
PHP 5.4.0 ,The constants ENT_HTML401 / ENT_XML1 / ENT_XHTML / ENT_HTML5 were introduced.
[/table]


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


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

In above example ,We have a string ‘&lt;a href=&quot;http://www.tutorialmines.net&quot;&gt;Click to go tutorialmines.net&lt;/a&gt;’; and string ‘&lt;b&gt;&lt;i&gt;www.tutorialmines.net. I am bold and italic.&lt;/i&gt;&lt;/b&gt;’;. Now see how the functions html_entity_decode() will decode them to applicable HTML characters .
See below is the HTML output of above code view source of browser.

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

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

Click to go tutorialmines.net
www.tutorialmines.net. I am bold and italic.

html_entity_decode() – PHP Functions Example 2 :

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

<?php
$strExample = "We&#039;re here to help you to learn PHP. &quot; using double quotes also.";
echo html_entity_decode($strExample , ENT_COMPAT); // This will only convert double quotes
echo "<br/>";
echo html_entity_decode($strExample , ENT_QUOTES); // This will converts double and single quotes
echo "<br/>";
echo html_entity_decode($strExample , 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&#039;re here to help you to learn PHP. " using double quotes also.<br/>We're here to help you to learn PHP. " using double quotes also.<br/>We&#039;re here to help you to learn PHP. &quot; 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.

html_entity_decode() – PHP Functions Example 3 :

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

<?php
$strExample = "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.";
echo html_entity_decode($strExample , 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 ! <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.

</body>
</html>

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

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

You may also like...

Leave a Reply

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