html_entity_decode
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
Convert HTML entities to their corresponding characters
Description
html_entity_decode() is the opposite of htmlentities() in that it converts HTML entities in the string to their corresponding characters.
More precisely, this function decodes all the entities (including all numeric entities) that a) are necessarily valid for the chosen document type — i.e., for XML, this function does not decode named entities that might be defined in some DTD — and b) whose character or characters are in the coded character set associated with the chosen encoding and are permitted in the chosen document type. All other entities are left as is.
Parameters
stringThe input string.
flagsA bitmask of one or more of the following flags, which specify how to handle quotes and which document type to use. The default is
ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.Constant Name Description ENT_COMPATWill convert double-quotes and leave single-quotes alone. ENT_QUOTESWill convert both double and single quotes. ENT_NOQUOTESWill leave both double and single quotes unconverted. ENT_SUBSTITUTEReplace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or � (otherwise) instead of returning an empty string. ENT_HTML401Handle code as HTML 4.01. ENT_XML1Handle code as XML 1. ENT_XHTMLHandle code as XHTML. ENT_HTML5Handle code as HTML 5. encodingEncoding Charsets
Return Values
Returns the decoded string.
Changelog
| Version | Description |
|---|---|
| 8.1.0 | flags changed from ENT_COMPAT to ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401. |
| 8.0.0 | encoding is nullable now. |
Examples
Decoding HTML entities
<?php
$orig = "I'll \"walk\" the <b>dog</b> now";
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a, PHP_EOL; // I'll "walk" the <b>dog</b> now
echo $b, PHP_EOL; // I'll "walk" the <b>dog</b> now
?>Notes
You might wonder why trim(html_entity_decode(' ')); doesn't reduce the string to an empty string, that's because the ' ' entity is not ASCII code 32 (which is stripped by trim()) but ASCII code 160 (0xa0) in the default ISO 8859-1 encoding.