PHP htmlspecialchars_decode() Function

What is PHP htmlspecialchars_decode() Function?

If you want to convert some HTML entity to their to their corresponding characters, use html_entity_decode() function. So, the function converts HTML entity “&lt;” to its corresponding character <.

The function converts(decode) the following 5 HTML entities into their corresponding characters-

HTML entitiesCharacter
&amp;& (ampersand)
&quot;" (double quote)
&#039; (or &apos; depending on flags/PHP version)' (single quote)
&lt;< (less than)
&gt;> (greater than)

Syntax:

htmlspecialchars_decode(string, flags)

Parameters:

The Function has 1 required parameter and 2 optional parameters-

string (Required): It specifies a string that you want to convert to HTML entity.

flags (Optional): It specifies how to handle quotes, invalid encoding and the used document type. The default is ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.

Return Values:

The function returns the converted (decoded) string.

Examples:

Example 1:

<?php
$html_entities = "Learn Web Development from &lt;strong&gt;&lt;u&gt;Schools of Web - SOW&lt;/u&gt;&lt;/strong&gt;.";
$decoded_string = htmlspecialchars_decode($html_entities);
echo $decoded_string;
?>

Output:

Learn Web Development from Schools of Web - SOW

Example 2:

<?php
$string = "They&#039;re learning PHP";
$decoded_string = htmlspecialchars_decode($string, ENT_NOQUOTES);
echo "Decoded string is: " . $decoded_string;
?>

Output:

Decoded string is: They&#039;re learning PHP

Explanation:

If you see the view source of the browser, you’ll see this.

Notes:

It is the opposite of the htmlspecialchars() function.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP htmlspecialchars_decode() Function

htmlspecialchars _decode() function is one of the built-in string functions. Use this function to decode those selected HTML entities to their equivalent characters.

Reference:

https://www.php.net/manual/en/function.htmlspecialchars-decode.php