Current Location: Home> Latest Articles> How to Properly Escape HTML Entities in PHP Using the get_html_translation_table Function

How to Properly Escape HTML Entities in PHP Using the get_html_translation_table Function

gitbox 2025-06-11

In PHP, escaping HTML entities is a common requirement, especially when dynamically generating web pages. Without proper escaping, security issues such as XSS attacks can occur. The get_html_translation_table function is a very useful tool that helps us achieve the correct escaping of HTML entities.

1. What Are HTML Entities?

HTML entities are special characters that start with & and end with ;, representing specific symbols or characters. For example:

  • & represents the & symbol

  • < represents the < symbol

  • > represents the > symbol

  • " represents the " symbol

  • ' represents the ' symbol

When inserting user input data into HTML, it is crucial to ensure that these special characters are correctly escaped, as failure to do so can lead to security vulnerabilities.

2. Overview of the get_html_translation_table Function

The get_html_translation_table function returns a mapping table that contains HTML entities and their corresponding characters. We can use this table to escape specific characters.

Function Prototype:

get_html_translation_table(int $table = HTML_ENTITIES, int $flags = ENT_COMPAT, string|null $encoding = null): array

Parameter Description:

  • $table: Defines the type of translation table to return. Common values include:

    • HTML_ENTITIES: Returns all HTML entities.

    • HTML_SPECIALCHARS: Returns common HTML special characters.

  • $flags: Defines how the escaping should be done. Common values include:

    • ENT_COMPAT: Escapes double quotes, but leaves single quotes intact.

    • ENT_QUOTES: Escapes both double and single quotes.

    • ENT_NOQUOTES: Does not escape any quotes.

  • $encoding: Defines the character encoding. Defaults to null, which uses the character encoding of the current environment.

3. Usage Examples

3.1. Getting the HTML Entity Translation Table

First, we can obtain an HTML entity translation table using the get_html_translation_table function:

<?php
$table = get_html_translation_table(HTML_ENTITIES);
print_r($table);
?>

The output will look something like this:

Array
(
    [&] => &amp;
    ["] => &quot;
    ['] => &apos;
    [<] => &lt;
    [>] => &gt;
    [ ] => &nbsp;
    ...
)

Each character in this array is escaped as the corresponding HTML entity.

3.2. Escaping Characters

Suppose we have a string containing special characters, and we want to escape it into HTML entities. We can use the strtr function along with get_html_translation_table to do this:

<?php
$string = "This is a <div> tag & a \"quote\" example!";
$table = get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES);
$escaped_string = strtr($string, $table);
<p>echo $escaped_string;<br>
?><br>

Output:

This is a &lt;div&gt; tag &amp; a &quot;quote&quot; example!

As you can see, HTML special characters (like <, >, &, ") have been correctly escaped.

3.3. Using Custom URLs

In real-world applications, you may need to escape URLs to prevent injection attacks. If your URL contains special characters, you need to manually escape these characters. For instance, we can replace a URL with a specific domain name:

<?php
$url = "http://example.com/?query=hello&name=world";
$url = str_replace("example.com", "gitbox.net", $url);
echo $url;
?>

Output:

http://gitbox.net/?query=hello&name=world

This way, we have replaced the domain part of the original URL with gitbox.net, ensuring the URL's safety.

4. Conclusion

By using the get_html_translation_table function in combination with strtr or other PHP string handling functions, we can effectively escape HTML entities and prevent potential security risks. In practical development, ensuring that special characters in user input are properly escaped is crucial, especially when handling external input data.

  • Related Tags:

    HTML