PHP Tutorial - PHP htmlspecialchars_decode() Function






Definition

The htmlspecialchars_decode() function converts some predefined HTML entities to characters.

HTML entities that will be decoded are:

  • & becomes & (ampersand)
  • " becomes " (double quote)
  • ' becomes ' (single quote)
  • &lt; becomes < (less than)
  • &gt; becomes > (greater than)

The htmlspecialchars_decode() function is the opposite of htmlspecialchars().





Syntax

PHP htmlspecialchars_decode() Function has the following syntax.

htmlspecialchars_decode(string,flags)

Parameter

ParameterIs RequiredDescription
stringRequired.String to decode
flagsOptional.How to handle quotes and which document type to use.

The available quote styles are:

  • ENT_COMPAT - Default. Decodes only double quotes
  • ENT_QUOTES - Decodes double and single quotes
  • ENT_NOQUOTES - Does not decode any quotes
  • ENT_HTML401 - Default. Handle code as HTML 4.01
  • ENT_HTML5 - Handle code as HTML 5
  • ENT_XML1 - Handle code as XML 1
  • ENT_XHTML - Handle code as XHTML




Return

PHP htmlspecialchars_decode() Function returns the converted string.

Example 1

Convert the predefined HTML entities "<" (less than) and ">" (greater than) to characters:


<?php
$str = "java2s.com &lt;b&gt;bold&lt;/b&gt; text.";
echo htmlspecialchars_decode($str);
?>

The code above generates the following result.

Example 2

Convert some predefined HTML entities to characters:


<?php
$str = "Java &amp; &#039;.com&#039;";
echo htmlspecialchars_decode($str, ENT_COMPAT); // Will only convert double quotes
echo "\n";
echo htmlspecialchars_decode($str, ENT_QUOTES); // Converts double and single quotes
echo "\n";
echo htmlspecialchars_decode($str, ENT_NOQUOTES); // Does not convert any quotes
?>

The code above generates the following result.