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 an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.