Current Location: Home> Latest Articles> How to Achieve Bidirectional Conversion of HTML Entities Using html_entity_decode and htmlentities?

How to Achieve Bidirectional Conversion of HTML Entities Using html_entity_decode and htmlentities?

gitbox 2025-06-15

When dealing with web content, it's common to need to convert special characters in HTML code to entities (such as turning & into &) or revert HTML entities back to regular characters. PHP provides two very useful functions to accomplish this task: htmlentities and html_entity_decode.

This article will introduce the basic usage of these two functions and demonstrate how to achieve bidirectional conversion of HTML entities using them.


1. htmlentities — Convert Characters to HTML Entities

The htmlentities function converts certain special characters in a string into their corresponding HTML entities, preventing the browser from interpreting them as tags or other meanings, thus ensuring the security and correct display of the page.

Function Prototype:

string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )

Simple Example:

<?php
$text = "Tom & Jerry&#039;s <b>adventure</b>!";
echo htmlentities($text);
// Output: Tom &amp; Jerry&apos;s &lt;b&gt;adventure&lt;/b&gt;!
?>

2. html_entity_decode — Convert HTML Entities Back to Characters

The function html_entity_decode does the opposite — it converts HTML entities back to their corresponding characters.

Function Prototype:

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

Simple Example:

<?php
$encoded = "Tom &amp; Jerry&apos;s &lt;b&gt;adventure&lt;/b&gt;!";
echo html_entity_decode($encoded);
// Output: Tom & Jerry&#039;s <b>adventure</b>!
?>

3. Example of Achieving Bidirectional Conversion of HTML Entities

Suppose we have a string that we first need to convert to HTML entities and store, and then later need to revert the entities back to the original characters. This can be achieved by combining these two functions.

<?php
$original = &#039;Hello & welcome to <a href="https://gitbox.net">gitbox.net</a>!&#039;;
<p>// Convert the string to HTML entities<br>
$encoded = htmlentities($original);<br>
echo "Encoded: " . $encoded . "\n";</p>
<p>// Convert the encoded string back<br>
$decoded = html_entity_decode($encoded);<br>
echo "Decoded: " . $decoded . "\n";<br>
?><br>

Output:

Encoded: Hello &amp; welcome to &lt;a href=&quot;https://gitbox.net&quot;&gt;gitbox.net&lt;/a&gt;!
Decoded: Hello & welcome to <a href="https://gitbox.net">gitbox.net</a>!

4. Common Parameter Descriptions

  • flags: Controls the type of entities to convert, such as ENT_QUOTES, which converts both single and double quotes.

  • encoding: Specifies the character encoding, which defaults to the encoding set in PHP's configuration. It’s generally safer to set this to UTF-8.

  • double_encode (specific to htmlentities): Specifies whether already converted entities should be encoded again. The default is true, but setting it to false avoids double encoding.


5. Conclusion

  • The htmlentities function prevents HTML tags from being interpreted by the browser, ensuring characters are safely output.

  • The html_entity_decode function reverts HTML entities back to their original characters, making further processing easier.

  • By combining bidirectional conversion, we can safely handle user input, preventing XSS attacks while also displaying content conveniently.

We hope this article helps you understand and effectively use these two functions to achieve bidirectional conversion of HTML entities.