Home PHP String Functions html_entity_decode() PHP html_entity_decode() Function 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.
$text = html_entity_decode($html, ENT_QUOTES, 'UTF-8'); Call What 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 html_entity_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null): string Parameters Parameter Description string Required. Text that may contain HTML entities. flags Optional. Quote/entity flags. Default depends on PHP version. encoding Optional. Character encoding. Prefer UTF-8.
Return Value Return Value Description string A string with entities decoded to characters.
PHP Examples 1. Basic decode 2. Round-trip 3. Quotes + UTF-8 Example 1 — Decode Common Entities Turn entity-encoded tags and a copyright symbol back into characters.
<?php
echo html_entity_decode("<b>Hi</b>") . "\n";
echo html_entity_decode("© 2026");
// <b>Hi</b>
// © 2026 How It Works Named entities. < / > become angle brackets.Symbols. © becomes the copyright character.Plain text. Letters like Hi stay unchanged.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
$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";
// <a href="x">Hi</a>
// same <a href="x">Hi</a>
same How It Works Encode. htmlentities() turns <, >, and quotes into entities.Decode. html_entity_decode() with the same flags restores characters.Match flags. ENT_QUOTES on both sides keeps quote handling consistent.UTF-8. Passing "UTF-8" avoids charset surprises.Example 3 — Decode Quotes Explicitly Use ENT_QUOTES so both " and ' / '-style quotes decode as expected.
<?php
$s = ""Hi" and 'Bye'";
echo html_entity_decode($s, ENT_QUOTES, "UTF-8");
// "Hi" and 'Bye' How It Works Double quotes. " becomes ".Single quotes. Numeric ' becomes ' with ENT_QUOTES.Explicit flags. Avoid relying only on version defaults (PHP 8.1+ changed them).Charset. "UTF-8" keeps multi-byte text correct.Frequently Asked Questions What does PHP html_entity_decode() do? html_entity_decode() converts HTML entities in a string back to their corresponding characters. For example, < becomes < and © becomes ©. It is the reverse of htmlentities().
What is the syntax for html_entity_decode()? 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).
What is the difference between html_entity_decode() and htmlspecialchars_decode()? html_entity_decode() reverses htmlentities() and decodes many named entities (including symbols). htmlspecialchars_decode() only reverses htmlspecialchars()—a smaller set: &, <, >, and quotes.
What ENT flags should I use? 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.
Is it safe to decode user input before displaying it in HTML? Only if you re-escape before output or use the text in a non-HTML context. Decoding <script> back to <script> and echoing it enables XSS. Decode for processing; escape again when rendering.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion