PHP html_entity_decode() function

Example

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

<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars_decode($str);
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>

Browser output of the above code:

This is some bold text.

Definition and Usage

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

The HTML entities that will be decoded are:

  • & Decoded to & (ampersand)
  • " Decoded to " (double quote)
  • ' Decoded to ' (single quote)
  • < Decoded to < (less than)
  • > Decoded to > (greater than)

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

Syntax

htmlspecialchars_decode(string,flags)
Parameter Description
string Required. Specifies the string to be decoded.
flags

Optional. Specifies how quotes are handled and which document type is used.

Available quote types:

  • ENT_COMPAT - Default. Decodes double quotes only.
  • ENT_QUOTES - Decodes both double and single quotes.
  • ENT_NOQUOTES - Does not decode any quotes.

Additional flags for the document type used:

  • ENT_HTML401 - Default. Treats code as HTML 4.01.
  • ENT_HTML5 - Treats code as HTML 5.
  • ENT_XML1 - Treats code as XML 1.
  • ENT_XHTML - Treats code as XHTML.

Technical Details

Return Value: Return the converted string.
PHP Version: 5.1.0+
Update Log:

In PHP 5.4, additional flags were added to specify the document type to be used:

  • ENT_HTML401
  • ENT_HTML5
  • ENT_XML1
  • ENT_XHTML

More Examples

Example 1

Convert predefined HTML entities to characters:

<?php
$str = "Bill & 'Steve'";
echo htmlspecialchars_decode($str, ENT_COMPAT); // Only convert double quotes
echo "<br>";
echo htmlspecialchars_decode($str, ENT_QUOTES); // Convert double quotes and single quotes
echo "<br>";
echo htmlspecialchars_decode($str, ENT_NOQUOTES); // Do not convert any quotes
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
Bill & 'Steve'<br>
Bill & 'Steve'<br>
Bill & 'Steve'
</body>
</html>

Browser output of the above code:

Bill & 'Steve'
Bill & 'Steve'
Bill & 'Steve'

Example 2

Convert predefined HTML entities to double quotes:

<?php
$str = 'I love "PHP".';
echo htmlspecialchars_decode($str, ENT_QUOTES); // Convert double quotes and single quotes
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
I love "PHP".
</body>
</html>

Browser output of the above code:

I love "PHP".