W3docs

html_entity_decode()

Our article is about the PHP function html_entity_decode(), which is used to convert HTML entities to their corresponding characters. This function is useful

The PHP html_entity_decode() function converts HTML entities (like &lt;, &amp;, or &copy;) back into their corresponding characters (<, &, ©). It is the inverse of htmlentities(), and it is what you reach for whenever you need to turn already-encoded HTML back into readable text — for example, when reading data that was stored in encoded form, or when processing the output of a system that escaped its HTML.

This page explains the syntax, each parameter, the flags that control how quotes are handled, and the common pitfalls (especially around security).

Syntax

html_entity_decode(
    string $string,
    int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401,
    ?string $encoding = null
): string
ParameterRequiredDescription
$stringYesThe input string containing the HTML entities to decode.
$flagsNoA bitmask that controls which quotes are decoded and which document type is assumed.
$encodingNoThe character encoding of the input. Defaults to the default_charset ini setting (UTF-8 on modern PHP).

The function returns the decoded string. Since PHP 8.1 the default for $flags is ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401; older versions defaulted to ENT_COMPAT | ENT_HTML401.

Basic example

php— editable, runs on the server

The &lt; and &gt; entities are turned back into < and >, so the output is:

<h1>Hello World</h1>

Controlling quotes with $flags

The $flags parameter decides how single and double quotes are treated:

FlagDecodes
ENT_COMPATDouble quotes only
ENT_QUOTESBoth single and double quotes
ENT_NOQUOTESNeither single nor double quotes

Add ENT_HTML401, ENT_HTML5, ENT_XML1, or ENT_XHTML to pick the entity set, and ENT_SUBSTITUTE/ENT_IGNORE to control how invalid byte sequences are handled. Use ENT_QUOTES when the input may contain encoded single quotes (&#039; / &apos;):

php— editable, runs on the server

With ENT_QUOTES, both the tags and the encoded single quote are restored:

<p>I'm a paragraph</p>

Specifying the character encoding

The third parameter, $encoding, tells PHP how to interpret the input bytes. It defaults to your default_charset ini setting. Pass it explicitly when you cannot rely on that default — most often "UTF-8":

php— editable, runs on the server

ENT_HTML5 lets PHP recognize the full set of named HTML5 entities, so &eacute; and &amp; are decoded to:

Café & Co.

Security: do not decode before output

html_entity_decode() is the opposite of escaping. Decoding user-supplied content and then printing it into a page reintroduces any <script> tags or attributes that escaping removed, opening an XSS hole. Decode only when you need the raw characters for non-HTML purposes (logging, comparison, generating a plain-text version, building a PDF, etc.). When you echo into HTML, keep the data escaped.

  • htmlentities() — the inverse: converts characters into HTML entities.
  • htmlspecialchars() — escapes only the five special HTML characters (<, >, &, ", ').
  • htmlspecialchars_decode() — reverses htmlspecialchars(); faster than html_entity_decode() when you only need those five characters back.

html_entity_decode() handles the full named-entity set, so use it when the input may contain entities like &copy; or &eacute;; reach for htmlspecialchars_decode() when you only ever produced output with htmlspecialchars().

Practice

Practice
What does the " `html_entity_decode()` function in PHP do?
What does the " `html_entity_decode()` function in PHP do?
Was this page helpful?