Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP String htmlspecialchars_decode() Function



The PHP String htmlspecialchars_decode() function is used to convert special HTML entities back to characters. The htmlspecialchars_decode() method is the inverse of htmlspecialchars(). The HTML entities to be decoded are:

  • & becomes & (ampersand)

  • " becomes " (double quote)

  • ' becomes ' (single quote)

  • &lt; becomes < (less than)

  • &gt; becomes > (greater than)

Syntax

Below is the syntax of the PHP String htmlspecialchars_decode() function −

string htmlspecialchars_decode ( string $string [, int $flags ] )

Parameters

Here are the parameters of the htmlspecialchars_decode() function −

  • $string − It is the information about input string.

  • $flags − It contains the information about flags.

Return Value

The htmlspecialchars_decode() function returns the decoded string.

PHP Version

First introduced in core PHP 5.1.0, the htmlspecialchars_decode() function continues to function easily in PHP 7, and PHP 8.

Example 1

First we will show you the simple example of the PHP String htmlspecialchars_decode() function to convert the special entities back to characters.

<?php
   // Define string here
   $str = "<p>tutorials -> </p>\n";
      
   // Use htmlspecialchars_decode() function and get the result
   echo htmlspecialchars_decode($str);
?>

Output

Here is the outcome of the following code −

<p>tutorials -> </p>

Example 2

In the below PHP code we will try to use the htmlspecialchars_decode() function and also provide the parameter ENT_QUOTES.

<?php
   // Define string here
   $str = 'I love "PHP".';

   // Use htmlspecialchars_decode() function and get the result
   echo htmlspecialchars_decode($str, ENT_QUOTES); 
?> 

Output

This will generate the below output −

I love "PHP".

Example 3

In the below program we will demonstrate how to specify a doctype, HTML5 in our case, while decoding HTML entities using the htmlspecialchars_decode() function.

<?php
   // Example string with HTML entities
   $string = "Hello <strong>world</strong> & welcome to HTML5!";

   // Decode with HTML5 doctype
   $decoded_string = htmlspecialchars_decode($string, ENT_HTML5);

   // Output the decoded string
   echo $decoded_string;
?> 

Output

This will create the below output −

Hello <strong>world</strong> & welcome to HTML5!
php_function_reference.htm
Advertisements