PHP htmlspecialchars_decode() Function

PHP htmlspecialchars_decode() Function converts the predefined HTML entities back to characters.

A predefined HTML entities are given as below.

  1. & will be converted to & (ampersand).
  2. " will be converted to " (double quote).
  3. ' will be converted to ' (single quote).
  4. &lt; will be converted to < (less than).
  5. &gt; will be converted to > (greater than).

Syntax :

htmlspecialchars_decode(string,flags)

Parameter,

string : Required. Input the string to decode.
flags :  Optional. It specifies how to handle quotes and which document type to use. The default is ENT_COMPAT | ENT_HTML401.

Available flags constants are given as below.

  • ENT_COMPAT- It will convert double-quotes and leave single-quotes alone.
  • ENT_QUOTES - It will convert both double and single quotes.
  • ENT_NOQUOTES- It will leave both double and single quotes unconverted.
  • ENT_HTML401 - It handles code as HTML 4.01.
  • ENT_XML1 -  It handles code as XML 1.
  • ENT_XHTML -  It handles code as XHTML.
  • ENT_HTML5 -  It handles code as HTML 5.

A htmlspecialchars_decode() returns decoded string.

Let's see below example to understand php htmlspecialchars_decode() Function in details.

Example :

<br><b>The browser output of the above code as below.</b>
<?php
echo "<br><br><b>Converts &amp; to characters </b>";
$str = "Learn PHP &amp; HTML";
echo "<br>".htmlspecialchars_decode($str);
echo "<br><br><b>Converts &quot; to characters </b>";
$str = 'Online tutorial is available for &quot;php&quot;';
echo "<br>".htmlspecialchars_decode($str);
echo "<br><br><b>Converts &#039; to characters </b>";
$str = "Online tutorial is available for &#039;php&#039;";
echo "<br>".htmlspecialchars_decode($str);
echo "<br><br><b>Converts &lt; and &gt; to characters </b>";
$str = "&lt;a href='https://www.aryatechno.com/blog.html'&gt;Learn online tutorials&lt;/a&gt;";
echo "<br>".htmlspecialchars_decode($str);

?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

61227