W3docs

get_html_translation_table()

The get_html_translation_table() function is used to return the translation table used by the htmlspecialchars() and htmlentities() functions. The syntax of the

The PHP get_html_translation_table() function returns the exact lookup table that htmlspecialchars() and htmlentities() use internally when they convert characters to HTML entities. Instead of applying that conversion, this function hands you the table itself as an associative array, where each key is a raw character and each value is its entity — for example '<' maps to &lt;.

This is mainly useful when you need to inspect which characters get encoded, customize the encoding, or reverse it by flipping the table.

Syntax

get_html_translation_table(
    int $table = HTML_SPECIALCHARS,
    int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401,
    string $encoding = "UTF-8"
): array

All three parameters are optional:

  • $table — which table to return. One of two constants:
    • HTML_SPECIALCHARS (default) — the small set used by htmlspecialchars(): &, ", ', <, >.
    • HTML_ENTITIES — the full set used by htmlentities(), covering hundreds of characters such as ©, é, and ¡.
  • $flags — controls quote handling and the document type (see Quote and document flags below).
  • $encoding — the character encoding (for example "UTF-8" or "ISO-8859-1"). It affects which byte sequences the characters in the returned table map from.

The function returns an array. It never returns false, so there is no error case to handle for valid arguments.

Returning the htmlspecialchars table

With the default HTML_SPECIALCHARS table you get the five characters that matter most for safe HTML output:

php— editable, runs on the server

Output:

Array
(
    ["] => &quot;
    [&] => &amp;
    ['] => &#039;
    [<] => &lt;
    [>] => &gt;
)

Because ENT_QUOTES was passed, both the double quote (") and the single quote (') appear in the table. With the default ENT_COMPAT, the single quote would be missing.

Returning the full htmlentities table

Passing HTML_ENTITIES returns the complete table — over 250 entries, including accented letters and symbols:

php— editable, runs on the server

The first few entries look like this:

Array
(
    ["] => &quot;
    [&] => &amp;
    ['] => &#039;
    [<] => &lt;
    [>] => &gt;
    [ ] => &nbsp;
    [¡] => &iexcl;
    [¢] => &cent;
    ...
)

This is the table htmlentities() walks through to convert text. If you only care about the markup-significant characters, prefer HTML_SPECIALCHARS — it is smaller and faster to scan.

Quote and document flags

The $flags parameter combines a quote-style flag with an optional document-type flag using the bitwise OR operator (|). The most common values are:

  • ENT_COMPAT — encode double quotes, leave single quotes untouched.
  • ENT_QUOTES — encode both double and single quotes.
  • ENT_NOQUOTES — leave both quote types untouched.
  • ENT_HTML401 — use the HTML 4.01 entity set (the default document type).
  • ENT_HTML5 — use the HTML5 entity set.
  • ENT_XML1 — use the XML 1.0 entity set.
  • ENT_XHTML — use the XHTML entity set.
php— editable, runs on the server

Here ENT_QUOTES | ENT_HTML401 asks for both quote types to be encoded using the HTML 4.01 entity names. Switching the document flag to ENT_HTML5 would change a handful of entity names (for example single quote becomes &apos; instead of &#039;).

Practical use: reversing the table

A common real-world reason to fetch the table is to build a decode map by flipping keys and values with array_flip(). You can then turn entities back into raw characters with strtr():

<?php
$table   = get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES);
$flipped = array_flip($table);

echo strtr("&lt;b&gt;Bold &amp; bright&lt;/b&gt;", $flipped);
// Output: <b>Bold & bright</b>
?>

For everyday decoding you would normally reach for the dedicated htmlspecialchars_decode() or html_entity_decode() functions — but the flipped table is handy when you need a custom or partial mapping.

When to use it

Reach for get_html_translation_table() when you want to:

  • Audit exactly which characters a given flag/encoding combination will encode.
  • Build a custom or reversed mapping that the standard encode/decode functions do not cover.
  • Generate documentation or tests that list every entity conversion.

For simply encoding output, use htmlspecialchars() or htmlentities() directly instead.

Practice

Practice
What does the get_html_translation_table() function in PHP do?
What does the get_html_translation_table() function in PHP do?
Was this page helpful?