Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท String Functions

PHP htmlspecialchars_decode() and html_entity_decode()

Reference for PHP htmlspecialchars_decode() and html_entity_decode(): turn HTML entities back into characters, the flags, numeric entities and the safety pitfalls.

Two functions turn HTML entities back into ordinary characters. htmlspecialchars_decode() is the exact inverse of htmlspecialchars(): it converts only &, <, > and the quote entities. html_entity_decode() is broader and converts every named entity it knows (such as é, € or ©) and numeric entities such as é or €.

Decoding is rarely needed for output. Its typical uses are cleaning text that was escaped twice, reading text out of scraped or feed content, and preparing HTML text for a plain-text destination such as an email subject.

PHP
<?php
echo htmlspecialchars_decode("&lt;b&gt;Tom &amp; Jerry&lt;/b&gt;");
// <b>Tom & Jerry</b>
echo html_entity_decode("caf&eacute; &euro;5");
// cafรฉ โ‚ฌ5

Try it

PHP Special characters versus all entities

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Quotes, flags and double escaping

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
htmlspecialchars_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401): string
html_entity_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null): string
Name Description
$string The text containing entities.
$flags Which quote entities to decode and which entity table to use (ENT_HTML401, ENT_HTML5, ENT_XML1, ENT_XHTML).
$encoding Character set of the result (html_entity_decode() only). null uses default_charset, normally UTF-8.

Return value: the decoded string.

Usage notes

  • Quote handling follows the flags. With ENT_NOQUOTES neither quote entity is decoded; with ENT_COMPAT only &quot;; with ENT_QUOTES both. &apos; is an HTML5 entity, so it is decoded only when the table is ENT_HTML5 (or ENT_XML1/ENT_XHTML).
  • Choose the right function. After htmlspecialchars() use htmlspecialchars_decode(). html_entity_decode() would also change entities that the original text legitimately contained, such as a literal &eacute; that a user typed on purpose.
  • Decoding output is dangerous. Decoded text may contain <script> again. Never decode and then print into a page without escaping once more.
  • Do not decode to "fix" security escaping. If you find yourself decoding before every display, escape at output instead of at input.
  • Single pass. Decoding is not recursive. &amp;lt; becomes &lt;, not <, after one call.
  • Invalid code points. Numeric entities that are not valid in the target encoding are left unchanged.
  • html_entity_decode() is not an HTML parser. To extract text from markup use strip_tags() first, then decode.
  • htmlentities() is the inverse of html_entity_decode().