PHP html_entity_decode() function

Example

Convert HTML entities to characters:

<?php
$str = "<© W3Sçh°°¦§>";
echo html_entity_decode($str);
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
<? W3S?h????>
</body>
</html>

Browser output of the above code:

<? W3S?h????>

Definition and Usage

The html_entity_decode() function converts HTML entities to characters.

The html_entity_decode() function is htmlentities() Inverse function of the function.

Syntax

html_entity_decode(string,flags,character-set)
Parameter Description
string Required. Specify the string to be decoded.
flags

Optional. Specify how quotes are handled and which document type to use.

Available quote types:

  • ENT_COMPAT - Default. Decode double quotes only.
  • ENT_QUOTES - Decode both double and single quotes.
  • ENT_NOQUOTES - Do not decode any quotes.

Specify additional flags for the document type used:

  • ENT_HTML401 - Default. Treat as HTML 4.01.
  • ENT_HTML5 - Treat as HTML 5.
  • ENT_XML1 - Treat as XML 1.
  • ENT_XHTML - Treat as XHTML.
character-set

Optional. String value, specifying the character set to be used.

Allowed values:

  • UTF-8 - Default. 8-bit Unicode compatible with ASCII
  • ISO-8859-1 - Western Europe
  • ISO-8859-15 - Western Europe (includes euro symbol + missing French and Finnish letters from ISO-8859-1)
  • cp866 - Cyrillic character set for DOS
  • cp1251 - Cyrillic character set for Windows
  • cp1252 - West European character set for Windows
  • KOI8-R - Russian
  • BIG5 - Traditional Chinese, mainly used in Taiwan
  • GB2312 - Simplified Chinese, national standard character set
  • BIG5-HKSCS - BIG5 with Hong Kong extension
  • Shift_JIS - Japanese
  • EUC-JP - Japanese
  • MacRoman - Character set used by the Mac operating system

Note:In versions of PHP prior to 5.4, unrecognized character sets were ignored and replaced by ISO-8859-1. Starting from PHP 5.4, unrecognized character sets were ignored and replaced by UTF-8.

Technical details

Return value: Returns the converted string
PHP version: 4.3.0+

Update log:

Version Description
PHP 5 character-set The default parameter value has been changed to UTF-8.
PHP 5.4

Additional flags have been added to specify the document types for which the translation table is applicable:

  • ENT_HTML401
  • ENT_HTML5
  • ENT_XML1
  • ENT_XHTML
PHP 5.3.4 New support for multi-byte encoding added.

More Examples

Example 1

Convert HTML entities to characters:

<?php
$str = "Bill & 'Steve'";
echo html_entity_decode($str, ENT_COMPAT); // Only convert double quotes
echo "<br>";
echo html_entity_decode($str, ENT_QUOTES); // Convert double and single quotes
echo "<br>";
echo html_entity_decode($str, ENT_NOQUOTES); // Do not convert any quotes
?>

HTML output of the above code (view source code):

<!DOCTYPE html>
<html>
<body>
Bill & 'Steve'<br>
Bill & 'Steve'<br>
Bill & 'Steve'
</body>
</html>

Browser output of the above code:

Bill & 'Steve'
Bill & 'Steve'
Bill & 'Steve'

Example 2

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

<?php
$str = "My name is Øyvind Åsane. I'm Norwegian.";
echo html_entity_decode($str, ENT_QUOTES, "ISO-8859-1");
?>

HTML output of the above code (view source code):

<!DOCTYPE html>
<html>
<body>
My name is ?yvind ?sane. I'm Norwegian.
</body>
</html>

Browser output of the above code:

My name is ?yvind ?sane. I'm Norwegian.