PHP html_entity_decode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, html_entity_decode() converts HTML entities such as & back into characters.

The string is required. Flags and encoding are optional and control quotes and character set handling.

Syntax

Call html_entity_decode on the string. Start with this line.

PHP
$text = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
CallWhat it does
html_entity_decode("&")Returns "&".
htmlentities($text)Encodes characters into entities.

This is the reverse of htmlentities(). Choose flags that match how the text was encoded.

Library declarations
PHP
html_entity_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null): string

Parameters

ParameterDescription
stringRequired. Text that may contain HTML entities.
flagsOptional. Quote/entity flags. Default depends on PHP version.
encodingOptional. Character encoding. Prefer UTF-8.

Return Value

Return ValueDescription
stringA string with entities decoded to characters.

PHP Examples

Example 1 — Decode Common Entities

Turn entity-encoded tags and a copyright symbol back into characters.

PHP
<?php
echo html_entity_decode("&lt;b&gt;Hi&lt;/b&gt;") . "\n";
echo html_entity_decode("&copy; 2026");
// <b>Hi</b>
// © 2026

How It Works

  1. Named entities. &lt; / &gt; become angle brackets.
  2. Symbols. &copy; becomes the copyright character.
  3. Plain text. Letters like Hi stay unchanged.
  4. Output care. The result contains real < tags—escape again before HTML display.

Example 2 — Encode, Then Decode

Round-trip with htmlentities() restores the original string when flags and encoding match.

PHP
<?php
$original = '<a href="x">Hi</a>';
$encoded = htmlentities($original, ENT_QUOTES, "UTF-8");
$decoded = html_entity_decode($encoded, ENT_QUOTES, "UTF-8");

echo $encoded . "\n";
echo ($decoded === $original) ? "same" : "diff";
// &lt;a href=&quot;x&quot;&gt;Hi&lt;/a&gt;
// same

How It Works

  1. Encode. htmlentities() turns <, >, and quotes into entities.
  2. Decode. html_entity_decode() with the same flags restores characters.
  3. Match flags. ENT_QUOTES on both sides keeps quote handling consistent.
  4. UTF-8. Passing "UTF-8" avoids charset surprises.

Example 3 — Decode Quotes Explicitly

Use ENT_QUOTES so both &quot; and &#039; / &apos;-style quotes decode as expected.

PHP
<?php
$s = "&quot;Hi&quot; and &#039;Bye&#039;";
echo html_entity_decode($s, ENT_QUOTES, "UTF-8");
// "Hi" and 'Bye'

How It Works

  1. Double quotes. &quot; becomes ".
  2. Single quotes. Numeric &#039; becomes ' with ENT_QUOTES.
  3. Explicit flags. Avoid relying only on version defaults (PHP 8.1+ changed them).
  4. Charset. "UTF-8" keeps multi-byte text correct.

Frequently Asked Questions

html_entity_decode() converts HTML entities in a string back to their corresponding characters. For example, &lt; becomes < and &copy; becomes ©. It is the reverse of htmlentities().
string html_entity_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null). Pass the entity-encoded string, optional ENT_* flags, and an encoding (UTF-8 recommended).
html_entity_decode() reverses htmlentities() and decodes many named entities (including symbols). htmlspecialchars_decode() only reverses htmlspecialchars()—a smaller set: &, <, >, and quotes.
Match the flags used when encoding. ENT_QUOTES decodes both single and double quote entities. Use ENT_HTML5 when you need HTML5 entities. Always pass UTF-8 for modern apps.
Only if you re-escape before output or use the text in a non-HTML context. Decoding &lt;script&gt; back to <script> and echoing it enables XSS. Decode for processing; escape again when rendering.

Next: htmlentities()

Learn how to encode characters as HTML entities for safe output.

htmlentities tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful