PHP htmlspecialchars_decode | HTML Entities to Characters

PHP htmlspecialchars_decode is an inbuilt function in PHP. It converts HTML Entities back to plain text characters. In this article, we will discuss the PHP htmlspecialchars_decode function. Also, we will discuss a few examples of using it.

This function is the opposite of htmlspecialchars function. You can encode characters to HTML entities using htmlspecialchars function.

Syntax

htmlspecialchars_decode ($string, $flags)

Parameters

The PHP htmlspecialchars_decode expects two parameters. However, only one parameter is mandatory and the other is optional. The description of the parameters is as follows:

  • $string: The string to decode. Also, it is a mandatory parameter.
  • $flags: The second parameter is a combination of two flags and it specifies the behavior with handling quotes. The default is ENT_COMPAT | ENT_HTML401.

Also, you can view the list of supported flags and encoding value on the Official PHP Documentation.

Return Value

The PHP htmlspecialchars_decode Function returns the string after converting HTML entities back to original characters. It makes the following translations:

  • & becomes & (ampersand)
  • " becomes ” (Double Quote)
  • ' becomes ‘ (Single Quote)
  • &lt; becomes < (Less than)
  • &gt; becomes > (Greater than)
PHP htmlspecialchars_decode Function

Examples

Let’s discuss a few examples of using the function.

Example 1: Converting Ampersand (&)

For instance, consider the following example.

<?php
	
	$string = "Concatly &amp; 'Knowledge'";
	echo htmlspecialchars_decode($string);

?>

In this example, the function outputs the string after converting &amp; to & (Ampersand).

Concatly & 'Knowledge'

Example 2: Decoding Less Than and Greater Than

Similarly, you can decode less than and greater than symbols using the function.

<?php
	
	$string = '10 &gt; 5 but 10 &lt; 20';
	echo htmlspecialchars_decode($string);

?>
OUTPUT:
10 > 5 but 10 < 20

Conclusion

In conclusion, we discussed the PHP htmlspecialchars_decode Function. You can read more about it on the Official PHP Documentation. Additionally, you can read more PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *