PHP String html_entity_decode() Function



The PHP String html_entity_decode() function is used to translate HTML entities into its corresponding characters. It accepts three parameters as argument - string, flags and encoding. This function is the inverse of htmlentities().

Syntax

Below is the syntax of the PHP String html_entity_decode() function −

string html_entity_decode ( string $string, int $flags, string $encoding )

Parameters

Below are the parameters of the html_entity_decode() function −

  • $string − It is the string to decode.

  • $flags − It is used to determine how to handle quotes and which document type to utilize.

  • $encoding − It is an optional parameter that specifies the encoding used when converting characters. If not set, encoding is set to the value of the default_charset configuration parameter.

Available Flags Constants

Here are the list of available flags constants −

Constant Name Description
ENT_COMPAT Converts double-quotes and leave single-quotes alone.
ENT_QUOTES Converts both double and single quotes.
ENT_NOQUOTES Leaves both double and single quotes unconverted.
ENT_SUBSTITUTE Used to replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) instead of returning an empty string.
ENT_HTML401 Handles code as HTML 4.01.
ENT_XML1 Handles code as XML 1.
ENT_XHTML Handles code as XHTML.
ENT_HTML5 Handles code as HTML 5.

Supported Character sets

The following character sets are supported in with this function −

Charset Aliases Description
ISO-8859-1 ISO8859-1 Western European, Latin-1.
ISO-8859-5 ISO8859-5 Little used cyrillic charset (Latin/Cyrillic).
ISO-8859-15 ISO8859-15 Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1 (ISO-8859-1).
UTF-8 ASCII compatible multi-byte 8-bit Unicode.
cp866 ibm866, 866 DOS-specific Cyrillic charset.
cp1251 Windows-1251, win-1251, 1251 Windows-specific Cyrillic charset.
cp1252 Windows-1252, 1252 Windows specific charset for Western European.
KOI8-R koi8-ru, koi8r Russian.
BIG5 950 Traditional Chinese, mainly used in Taiwan.
GB2312 936 Simplified Chinese, national standard character set.
BIG5-HKSCS Big5 with Hong Kong extensions, Traditional Chinese.
Shift_JIS SJIS, SJIS-win, cp932, 932 Japanese.
EUC-JP EUCJP, eucJP-win Japanese.
MacRoman Charset that was used by Mac OS.

Return Value

The html_entity_decode() function returns the decoded string.

PHP Version

First introduced in core PHP 4.3.0, the html_entity_decode() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

This code shows how to use the PHP String html_entity_decode() function to turn special characters in a string into HTML entities and then decode them back to their original form.

<?php
   $input = "tutorials \"point\" simply <b>easy</b> learning";
   $ab = htmlentities($input);
   $b = html_entity_decode($ab);

   echo $ab;
?>

Output

Here is the outcome of the following code −

tutorials "point" simply <b>easy</b> learning

Example 2

In the below PHP code we are using the html_entity_decode() function and show how quotes are decoded with the help of flags.

<?php
   // String with encoded quotes
   $input = "This is "quoted" text.";

   // Decode with ENT_QUOTES flag
   $ds = html_entity_decode($input, ENT_QUOTES);

   // Output the result
   echo $ds;
?> 

Output

This will generate the below output −

This is "quoted" text.

Example 3

Now the below code decodes entities with a specified encoding with the help of html_entity_decode() function and prints it.

<?php
   // Encoded string
   $input = "I love & enjoy coding.";

   // Decode using ISO-8859-1 encoding
   $ds = html_entity_decode($htmlString, ENT_NOQUOTES, "UTF-8");

   // Output the result
   echo $ds;
?> 

Output

This will create the below output −

I love & enjoy coding.

Example 4

This PHP program is used to decode HTML entities by keeping quotes encoded using the html_entity_decode() function.

<?php
   // String with quotes
   $string = "This is "quoted" text.";
   
   // Decode with ENT_QUOTES flag
   $input = html_entity_decode($string, ENT_QUOTES, "UTF-8");
   
   // Output the result
   echo $input;
?> 

Output

Following is the output of the above code −

This is "quoted" text.
php_function_reference.htm
Advertisements