How to Use htmlspecialchars_decode() Function in PHP 8.1 and 8.2 with Example?

Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.

In PHP, the `htmlspecialchars_decode()` function is used to convert special HTML entities back to their corresponding characters. This function is particularly useful when you want to decode HTML entities that have been encoded using the `htmlspecialchars()` or `htmlentities()` functions. Here’s how you can use `htmlspecialchars_decode()` in PHP 8.1 and 8.2 with an example:

<?php

// Example usage of htmlspecialchars_decode()

// HTML string with encoded entities
$html_encoded = "&lt;p&gt;This is &quot;example&quot; &amp; text&lt;/p&gt;";

// Decode HTML entities
$html_decoded = htmlspecialchars_decode($html_encoded);

// Output the decoded HTML
echo $html_decoded;

?>

Output:

<p>This is "example" & text</p>

In this example:
1. We have an HTML string `$html_encoded` containing encoded HTML entities.
2. We use `htmlspecialchars_decode()` function to decode the entities back to their corresponding characters.
3. Finally, we echo the decoded HTML string.

This function can be especially useful when you want to display HTML content that has been encoded for safe output or when dealing with data retrieved from a database or user input where HTML entities have been encoded for security reasons.