PHP html_entity_decode() Function

What is PHP html_entity_decode() Function?

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

Syntax:

html_entity_decode(string, flags, encoding)

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.

encoding (Optional): It specifies the encoding used when converting characters. If omitted, encoding defaults to the value of the default_charset configuration option. Although this argument is technically optional, you are highly encouraged to specify the correct value for your code if the default_charset configuration option may be set incorrectly for the given input.

Return Values:

The function returns the converted (decoded) string.

Examples:

Example 1:

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

Output:

Learn Web Development from Schools of Web - SOW

Example 2:

&lt;?php
$string = "They&amp;#039;re learning PHP";
$decoded_string = html_entity_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 htmlentities() function.

PHP Version Support:

PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8

Summary: PHP html_entity_decode() Function

html_entity_decode() function is one of the built-in string functions. Use this function to decode HTML entities to its equivalent characters.

Reference:

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