PHP get_html_translation_table() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, get_html_translation_table() returns the array used to translate characters into HTML entities.

Table and flags are optional. Use it to inspect what htmlspecialchars() or htmlentities() would convert.

Syntax

Call get_html_translation_table. Start with this line.

PHP
$table = get_html_translation_table(HTML_SPECIALCHARS);
CallWhat it does
get_html_translation_table(HTML_SPECIALCHARS)Map for htmlspecialchars().
get_html_translation_table(HTML_ENTITIES)Larger map for htmlentities().

Flags and encoding affect which characters appear in the table.

Library declarations
PHP
get_html_translation_table(int $table = HTML_SPECIALCHARS, int $flags = ENT_QUOTES | ENT_SUBSTITUTE, ?string $encoding = null): array

Parameters

ParameterDescription
tableOptional. HTML_SPECIALCHARS or HTML_ENTITIES. Default HTML_SPECIALCHARS.
flagsOptional. Quote and entity flags, similar to htmlspecialchars().
encodingOptional. Character encoding. Default is UTF-8 in modern PHP.

Return Value

Return ValueDescription
arrayAn array mapping characters to HTML entities.

PHP Examples

Example 1 — Peek at Special Characters

Fetch the small table and print a few common mappings.

PHP
<?php
$map = get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES);

foreach (["&", "<", ">", '"', "'"] as $ch) {
    echo $ch, " => ", $map[$ch], PHP_EOL;
}

How It Works

  1. Build the map. HTML_SPECIALCHARS + ENT_QUOTES.
  2. Keys are characters. Look up &, <, quotes, etc.
  3. Values are entities. Ready for HTML output.
  4. Apostrophe form. Often &#039; depending on flags/version.

Example 2 — Encode with strtr()

Apply the table to a string — same idea as htmlspecialchars.

PHP
<?php
$map = get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES);
$text = 'Tom & Jerry <3 "cartoons"';
$safe = strtr($text, $map);

echo $safe, PHP_EOL;
// Tom &amp; Jerry &lt;3 &quot;cartoons&quot;

How It Works

  1. Get the map once. Reuse it for many strings if needed.
  2. Pass text + map to strtr. Each matching character is replaced.
  3. & and < become safe. Quotes are encoded because of ENT_QUOTES.
  4. Match flags carefully. Same flags as htmlspecialchars for same results.

Example 3 — Compare Table Sizes

See how much larger HTML_ENTITIES is than HTML_SPECIALCHARS.

PHP
<?php
$small = get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES);
$large = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);

echo "SPECIALCHARS: ", count($small), PHP_EOL;
echo "ENTITIES:     ", count($large), PHP_EOL;
echo "Has é? ", isset($large["é"]) ? $large["é"] : "no", PHP_EOL;

How It Works

  1. Build both tables. Same flags, different $table constant.
  2. Count the keys. Specialchars stays tiny; entities is huge.
  3. Check an accented letter. Often present only in the entities map.
  4. Pick by need. XSS basics → specialchars; rich entity set → entities.

Frequently Asked Questions

It returns the character-to-entity mapping array used by htmlspecialchars() and htmlentities(). Keys are special characters; values are their HTML entity strings (for example '<' => '&lt;').
array get_html_translation_table(int $table = HTML_SPECIALCHARS, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, string $encoding = 'UTF-8'). Use HTML_SPECIALCHARS or HTML_ENTITIES for $table.
HTML_SPECIALCHARS is a small map for &, <, >, and quotes. HTML_ENTITIES is a large map covering many symbols and accented letters—what htmlentities() can encode.
Pass it to strtr(): strtr($string, get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES)). With matching flags this matches htmlspecialchars().
No. Build the table once before the loop and reuse the array. Rebuilding it every iteration wastes work.

Next: hebrev()

Convert logical Hebrew text to visual order.

hebrev() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful