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 echo htmlspecialchars_decode("<b>Tom & Jerry</b>"); // <b>Tom & Jerry</b> echo html_entity_decode("café €5"); // cafรฉ โฌ5
Try it
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.
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
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_NOQUOTESneither quote entity is decoded; withENT_COMPATonly"; withENT_QUOTESboth.'is an HTML5 entity, so it is decoded only when the table isENT_HTML5(orENT_XML1/ENT_XHTML). - Choose the right function. After
htmlspecialchars()usehtmlspecialchars_decode().html_entity_decode()would also change entities that the original text legitimately contained, such as a literalé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.
&lt;becomes<, 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 usestrip_tags()first, then decode.htmlentities()is the inverse ofhtml_entity_decode().