PHP Function Reference

PHP htmlspecialchars_decode() Function



The PHP htmlspecialchars_decode() function is the opposite of htmlspecialchars() function. It converts special HTML entities back to characters. The special HTML entities are:

  • & becomes & (Ampersand)
  • " becomes " (Double quote) (when ENT_NOQUOTES is not set)
  • ' becomes ' (Single quote) (when ENT_QUOTES is set)
  • &lt; becomes < (Less than)
  • &gt; becomes > (Greater than)

Syntax

htmlspecialchars_decode(string, flags)

Parameters

string Required. Specify the string to decode.
flags Optional. Specify how to handle quotes and which document type to use. The available flags constants are:
  • ENT_COMPAT: Converts double-quotes and leave single-quotes alone.
  • ENT_QUOTES: Converts both double and single quotes.
  • ENT_NOQUOTES: Leaves both double and single quotes unconverted.
  • ENT_HTML401: Handle code as HTML 4.01.
  • ENT_XML1: Handle code as XML 1.
  • ENT_XHTML: Handle code as XHTML.
  • ENT_HTML5: Handle code as HTML 5.
The default is ENT_COMPAT | ENT_HTML401.

Return Value

Returns the decoded string.

Example:

The example below shows the usage of htmlspecialchars_decode() function.

<?php
$str = "<p>this -&gt; &quot;</p>\n";

echo htmlspecialchars_decode($str);

//quotes are not converted
echo htmlspecialchars_decode($str, ENT_NOQUOTES);
?>

The output of the above code will be:

<p>this -> "</p>
<p>this -> &quot;</p>

Example:

Consider one more example to understand htmlspecialchars_decode() function concept.

<?php
$str = 'I love &quot;PHP&quot;.';

//converts double and single quotes
echo htmlspecialchars_decode($str, ENT_QUOTES);
?>

The output of the above code will be:

I love "PHP".

❮ PHP String Reference