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.
htmlspecialchars_decode ($string, $flags)
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.
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)
- < becomes < (Less than)
- > becomes > (Greater than)
Let’s discuss a few examples of using the function.
Example 1: Converting Ampersand (&)
For instance, consider the following example.
<?php $string = "Concatly & 'Knowledge'"; echo htmlspecialchars_decode($string); ?>
In this example, the function outputs the string after converting & 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 > 5 but 10 < 20'; echo htmlspecialchars_decode($string); ?>
OUTPUT: 10 > 5 but 10 < 20
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.