W3docs

html_entity_decode()

Our article is about the PHP function html_entity_decode(), which is used to convert HTML entities to their corresponding characters. This function is useful

Our article is about the PHP function html_entity_decode(), which is used to convert HTML entities to their corresponding characters. This function is useful for working with HTML data, such as data from an HTML form or an HTML file. In this article, we will discuss the syntax and usage of html_entity_decode(), as well as provide some examples.

The html_entity_decode() function is used to convert HTML entities to their corresponding characters. The syntax of the html_entity_decode() function is as follows:

The PHP syntax of html_entity_decode()

string html_entity_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") ]] )

The function takes one required parameter, $string, which is the string containing the HTML entities to decode. The function also has two optional parameters, $flags and $encoding.

Here is an example of how to use the html_entity_decode() function:

Example of PHP html_entity_decode()

<?php
$html_string = "&lt;h1&gt;Hello World&lt;/h1&gt;";
$decoded_string = html_entity_decode($html_string);
echo $decoded_string;
?>

In this example, we have a string variable $html_string containing some HTML code with entities. We use the html_entity_decode() function to convert the HTML entities to their corresponding characters.

The output of this code will be:


<h1>Hello World</h1>

As you can see, the function successfully decodes the HTML entities back to their original characters.

The html_entity_decode() function can also take a second parameter, $flags, which specifies how to handle quotes and which set of entities to use. The default value for $flags is ENT_COMPAT | ENT_HTML401. Note that ENT_COMPAT only decodes double quotes, whereas ENT_QUOTES decodes both single and double quotes. For robustness, you may also consider flags like ENT_IGNORE or ENT_SUBSTITUTE to handle invalid entities gracefully.

Here is an example of how to use the html_entity_decode() function with the ENT_QUOTES flag:

How to use PHP html_entity_decode()?

<?php
$html_string = "&lt;p&gt;I'm a paragraph&lt;/p&gt;";
$decoded_string = html_entity_decode($html_string, ENT_QUOTES);
echo $decoded_string;
?>

In this example, we have a string variable $html_string containing some HTML code with entities, including a single quote. We use the html_entity_decode() function with the ENT_QUOTES flag to convert the HTML entities to their corresponding characters, and to handle the single quote.

The output of this code will be:


<p>I'm a paragraph</p>

With the ENT_QUOTES flag, both the paragraph tags and the single quote are correctly restored.

The html_entity_decode() function can also take a third parameter, $encoding, which specifies the character encoding of the input string. The default value for $encoding is the value of the default_charset configuration option.

Here is an example of how to use the html_entity_decode() function with a specific encoding:

An example of how to use the html_entity_decode() function with a specific encoding

<?php
$html_string = "&lt;h1&gt;Hello World&lt;/h1&gt;";
$decoded_string = html_entity_decode($html_string, ENT_COMPAT | ENT_HTML401, "UTF-8");
echo $decoded_string;
?>

In this example, we have a string variable $html_string containing some HTML code with entities. We use the html_entity_decode() function with the UTF-8 encoding to convert the HTML entities to their corresponding characters, using the ENT_COMPAT | ENT_HTML401 flags.

The output of this code will be:


<h1>Hello World</h1>

The specified UTF-8 encoding ensures the entities are decoded accurately according to the character set.

The html_entity_decode() function is a useful tool for working with HTML data, such as data from an HTML form or an HTML file. It can help you convert HTML entities to their corresponding characters, making your code more versatile and flexible. By mastering this function, you can become a more proficient PHP developer.

We hope this article has been helpful in understanding the html_entity_decode() function in PHP.

Practice

Practice

What does the " `html_entity_decode()` function in PHP do?