PHP htmlspecialchars_decode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, htmlspecialchars_decode() converts special HTML entities back to <, >, &, and quotes.

The string is required. Flags are optional. Use html_entity_decode() when you need the full entity set.

Syntax

Call htmlspecialchars_decode on the string. Start with this line.

PHP
$text = htmlspecialchars_decode($html, ENT_QUOTES);
CallWhat it does
htmlspecialchars_decode("&lt;b&gt;")Returns "".
htmlspecialchars($text, ENT_QUOTES, 'UTF-8')Encodes the special characters again.

This reverses htmlspecialchars(), not the larger htmlentities() set.

Library declarations
PHP
htmlspecialchars_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401): string

Parameters

ParameterDescription
stringRequired. Text with special HTML entities.
flagsOptional. Quote handling flags.

Return Value

Return ValueDescription
stringA string with special entities decoded.

PHP Examples

Example 1 — Decode Tags and Ampersands

Turn special entities back into characters. &copy; would stay unchanged—this function only handles the htmlspecialchars set.

PHP
<?php
echo htmlspecialchars_decode("&lt;b&gt;Hi&lt;/b&gt; &amp; ©", ENT_QUOTES);
// <b>Hi</b> & ©

How It Works

  1. Tags. &lt; and &gt; become < and >.
  2. Ampersand. &amp; becomes &.
  3. Symbols. A literal © (already a character) is left alone.
  4. Flags. ENT_QUOTES prepares quote decoding when quote entities appear.

Example 2 — Decode Quotes with ENT_QUOTES

Both single and double quote entities become real quotes when ENT_QUOTES is set.

PHP
<?php
echo htmlspecialchars_decode("It&#039;s &quot;ok&quot;", ENT_QUOTES);
// It's "ok"

How It Works

  1. Single quote. &#039; becomes ' with ENT_QUOTES.
  2. Double quote. &quot; becomes ".
  3. Match encode. Use the same flags you passed to htmlspecialchars().
  4. Defaults. Pass flags explicitly—PHP 8.1+ changed default flag combinations.

Example 3 — Decode, Then Re-escape

Matching flags restore the encoded form after a round-trip.

PHP
<?php
$encoded = "&lt;scr" . "ipt&gt;";
$plain = htmlspecialchars_decode($encoded, ENT_QUOTES);
$again = htmlspecialchars($plain, ENT_QUOTES, "UTF-8");

echo ($again === $encoded) ? "same" : "diff";
// same

How It Works

  1. Decode. Special entities become real characters.
  2. Re-escape. htmlspecialchars() rebuilds the entity form.
  3. Same flags. ENT_QUOTES on both sides keeps quotes consistent.
  4. Strict match. === confirms an exact round-trip.

Frequently Asked Questions

htmlspecialchars_decode() converts special HTML entities back to their characters. It reverses htmlspecialchars() and decodes only &, <, >, and quote entities—not the full symbol set that html_entity_decode() handles.
string htmlspecialchars_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401). Pass the encoded string and optional ENT_* flags. There is no encoding parameter—unlike htmlspecialchars() and html_entity_decode().
htmlspecialchars_decode() reverses htmlspecialchars() and decodes only the core specials (&, <, >, quotes). html_entity_decode() reverses htmlentities() and also decodes named symbols like &copy; and &euro;.
Match the flags used during encoding. ENT_QUOTES decodes both single and double quote entities. ENT_COMPAT decodes double quotes only. ENT_NOQUOTES leaves quote entities unchanged.
Only if you re-escape before output or use the decoded text outside HTML. Decoding &lt;script&gt; back to a real tag and echoing it raw enables XSS. Decode for processing; use htmlspecialchars() when rendering to the page.

Next: htmlspecialchars()

Learn how to escape HTML specials for safe output.

htmlspecialchars 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