html_entity_decode

Convert HTML entities to their corresponding characters

&reftitle.description; `string` `html_entity_decode` `string` `string` `int` `flags` ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ``string``null```encoding`&null;

`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.

&reftitle.parameters;

`string`

- The input string.

`flags`

- A 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`.

  ## Available `flags` constants

  Constant Name | Description
  `ENT_COMPAT` | Will convert double-quotes and leave single-quotes alone.
  `ENT_QUOTES` | Will convert both double and single quotes.
  `ENT_NOQUOTES` | Will leave both double and single quotes unconverted.
  `ENT_SUBSTITUTE` | Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#xFFFD; (otherwise) instead of returning an empty string.
  `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.

`encoding`

- &strings.parameter.encoding; &reference.strings.charsets;
&reftitle.returnvalues;

Returns the decoded string.

&reftitle.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.

&reftitle.examples;

### Decoding HTML entities

    <?php
    $orig = "I'll \"walk\" the <b>dog</b> now";

    $a = htmlentities($orig);

    $b = html_entity_decode($a);

    echo $a; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now

    echo $b; // I'll "walk" the <b>dog</b> now
    ?>

&reftitle.notes;

You might wonder why trim(html_entity_decode('&nbsp;')); doesn't reduce the string to an empty string, that's because the '&nbsp;' entity is not ASCII code 32 (which is stripped by `trim`) but ASCII code 160 (0xa0) in the default ISO 8859-1 encoding.

&reftitle.seealso;

- `htmlentities`
- `htmlspecialchars`
- `get_html_translation_table`
- `urldecode`