PHP 8.5 htmlspecialchars_decode() Function With Example

htmlspecialchars_decode() is the direct opposite of htmlspecialchars(). It converts special HTML entities (like <, >, &, and quotes) back into their normal characters.

Function Signature

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

Parameters

→$string: The encoded string you want to decode.
→$flags: Controls how quotes are handled. Default is ENT_QUOTES (decodes both single and double quotes). You can use ENT_COMPAT (only double quotes) or ENT_NOQUOTES (leave quotes alone).

Example

<?php
$encoded = "&lt;p&gt;Hello &amp; Welcome&lt;/p&gt;";

// Default: decodes quotes and tags
echo htmlspecialchars_decode($encoded); 
// Output: <p>Hello & Welcome</p>

// ENT_NOQUOTES: leaves quotes as entities, but still decodes <, >, &
$encodedQuotes = "&quot;Hello&quot; &lt;World&gt;";
echo htmlspecialchars_decode($encodedQuotes, ENT_NOQUOTES);
// Output: &quot;Hello&quot; <World>
?>

Summary

Use htmlspecialchars_decode() when you need to display user-submitted content that was previously encoded for safety back into its readable form. For full HTML entity decoding (including named entities like &copy;), use html_entity_decode() instead.

3 Views