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 = "<p>Hello & Welcome</p>";// Default: decodes quotes and tags
echo htmlspecialchars_decode($encoded);
// Output: <p>Hello & Welcome</p>// ENT_NOQUOTES: leaves quotes as entities, but still decodes <, >, &
$encodedQuotes = ""Hello" <World>";
echo htmlspecialchars_decode($encodedQuotes, ENT_NOQUOTES);
// Output: "Hello" <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 ©), use html_entity_decode() instead.