PHP get_html_translation_table() Function

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
String Functions

What You’ll Learn

The get_html_translation_table() function returns the character → HTML entity map that powers htmlspecialchars() and htmlentities(). Inspect it to understand encoding, or pass it to strtr() for manual or customized escaping.

01

Entity Map

Char → entity array.

02

Two Tables

SPECIALCHARS / ENTITIES.

03

ENT Flags

Control quote encoding.

04

strtr() Encode

Manual translation.

05

htmlspecialchars

Uses same table.

06

Cache Table

Don’t rebuild in loops.

Definition and Usage

In PHP, get_html_translation_table() exposes the lookup array behind HTML encoding functions. Each key is a single character (such as < or &); each value is the entity string PHP would substitute (such as &lt; or &amp;).

💡
Beginner Tip

Most of the time you only need htmlspecialchars() directly. Reach for get_html_translation_table() when you want to see the mappings, merge custom replacements, or encode with strtr() in one pass.

📝 Syntax

The function accepts a table type, optional flags, and encoding:

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

Parameters

  • $tableHTML_SPECIALCHARS (small set: &, <, >, quotes) or HTML_ENTITIES (full entity set used by htmlentities()).
  • $flags — bitmask controlling quotes and document type. Common values: ENT_COMPAT, ENT_QUOTES, ENT_NOQUOTES, ENT_HTML401, ENT_HTML5.
  • $encoding — character encoding (default UTF-8).

Return Value

Returns an associative array: original characters as keys, HTML entity strings as values.

⚡ Quick Reference

Basic table
get_html_translation_table(HTML_SPECIALCHARS)

Core special chars

Full entities
get_html_translation_table(HTML_ENTITIES)

Large symbol map

Manual encode
strtr($s, $table)

Apply the map

Both quotes
..., ENT_QUOTES)

Encode ' and "

Examples Gallery

Run these examples in PHP 7+. Each demonstrates a common pattern beginners encounter with get_html_translation_table().

📚 Getting Started

Inspect the default HTML special-character map.

Example 1 — HTML_SPECIALCHARS Table (ENT_COMPAT)

The smallest useful map—the same core characters htmlspecialchars() protects in HTML.

PHP
<?php
$table = get_html_translation_table(HTML_SPECIALCHARS, ENT_COMPAT);

foreach ($table as $char => $entity) {
    echo json_encode($char) . " => $entity\n";
}
?>

How It Works

With ENT_COMPAT, double quotes are encoded but single quotes are left alone. These four mappings prevent broken HTML and XSS from raw <script> tags.

Example 2 — ENT_QUOTES Adds Single-Quote Encoding

Compare how flags change the table—important when output appears inside single-quoted HTML attributes.

PHP
<?php
$compat  = get_html_translation_table(HTML_SPECIALCHARS, ENT_COMPAT);
$quotes  = get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES);

echo "ENT_COMPAT entries:  " . count($compat) . "\n";
echo "ENT_QUOTES entries:  " . count($quotes) . "\n";
echo "Has apostrophe map:  " . (isset($quotes["'"]) ? "yes\n" : "no\n");
?>

How It Works

ENT_QUOTES adds a mapping for ' (usually to &#039;). Use the same flag here that you pass to htmlspecialchars() for consistent results.

📈 Practical Patterns

Table sizes, manual encoding, and customization.

Example 3 — HTML_SPECIALCHARS vs HTML_ENTITIES

See how much larger the full entity table is compared to the special-character subset.

PHP
<?php
$special = get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES);
$full    = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);

echo "HTML_SPECIALCHARS: " . count($special) . " entries\n";
echo "HTML_ENTITIES:     " . count($full) . " entries\n";

// Sample from full table
echo "Copyright sign: " . ($full["©"] ?? "n/a") . "\n";
?>

How It Works

Exact entity count varies by PHP version and flags. HTML_ENTITIES includes symbols like ©, €, and accented letters—use htmlentities() for that level of encoding.

Example 4 — Manual Encoding with strtr()

Apply the table yourself—the result should match htmlspecialchars() when flags align.

PHP
<?php
$text  = 'Tom & Jerry <script>alert("x")</script>';
$flags = ENT_QUOTES;

$table  = get_html_translation_table(HTML_SPECIALCHARS, $flags);
$manual = strtr($text, $table);
$built  = htmlspecialchars($text, $flags);

echo "Manual (strtr):     $manual\n";
echo "htmlspecialchars:   $built\n";
echo "Match:              " . ($manual === $built ? "yes\n" : "no\n");
?>

How It Works

strtr() replaces each key character with its entity value in one pass. This is exactly what htmlspecialchars() does internally with the same table.

Example 5 — Extend the Table with Custom Replacements

Merge extra mappings for special cases—then encode once with strtr(). Build the table outside the loop.

PHP
<?php
// Build once — reuse for many strings
$table = get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES);
$table["…"] = "&hellip;";  // custom: ellipsis
$table["—"] = "&mdash;";   // custom: em dash

$inputs = [
    "Wait… what?",
    "Range: 1—10",
    "Plain & simple",
];

foreach ($inputs as $line) {
    echo strtr($line, $table) . "\n";
}
?>

How It Works

Start with PHP’s official map, add your own key → entity pairs, and call strtr(). Cache the merged table before processing batches—calling get_html_translation_table() inside a loop is slow.

🚀 Common Use Cases

  • Debugging encoding — inspect which characters PHP converts and to which entities.
  • Custom sanitization — merge extra mappings before strtr().
  • Batch processing — build the table once, reuse for thousands of strings.
  • Learning htmlspecialchars() — see the exact map behind the function.
  • Word/document cleanup — extend the table for smart quotes and dashes from pasted text.

🧠 How get_html_translation_table() Works

1

You choose table and flags

Select HTML_SPECIALCHARS or HTML_ENTITIES, plus quote and document-type flags.

Input
2

PHP builds the map

PHP assembles the same associative array used by htmlspecialchars() / htmlentities() internally.

Transform
3

Array is returned

Use it directly, pass it to strtr(), or merge custom entries before encoding.

Output
=

Translation table

Ready for inspection, customization, or manual encoding.

📝 Notes

  • Only two valid $table values: HTML_SPECIALCHARS and HTML_ENTITIES (not HTML_QUOTE_SINGLE or similar).
  • Default flags changed in PHP 8.1 to ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.
  • Call once and reuse—building the table inside a loop is noticeably slow.
  • Entity forms match htmlspecialchars() / htmlentities() only—not every possible HTML encoding variant.
  • For normal page output, prefer htmlspecialchars($text, ENT_QUOTES, 'UTF-8') directly.

Conclusion

The get_html_translation_table() function reveals the character-to-entity map behind PHP’s HTML encoding functions. Use it to learn, debug, customize, or batch-encode with strtr().

Match your flags to htmlspecialchars(), cache the table outside loops, and prefer built-in encoding functions unless you genuinely need a custom map.

💡 Best Practices

✅ Do

  • Cache the table before processing many strings
  • Match flags with your htmlspecialchars() calls
  • Use HTML_SPECIALCHARS for typical web output
  • Use ENT_QUOTES when encoding for HTML attributes
  • Merge custom entries only when truly needed

❌ Don’t

  • Call get_html_translation_table() on every loop iteration
  • Confuse table constants with ENT_* quote flags
  • Assume the table decodes entities (use html_entity_decode())
  • Rely on this for full XSS protection without context-aware escaping
  • Hard-code entity strings when the table already provides them

Key Takeaways

Knowledge Unlocked

Five things to remember about get_html_translation_table()

Use these points whenever you work with HTML entity maps in PHP.

5
Core concepts
📈 02

Two Tables

SPECIALCHARS / ENTITIES.

Syntax
🛠 03

ENT Flags

Control quote encoding.

Flags
🔄 04

strtr() Pair

Manual encoding.

Usage
05

Cache Table

Build once, reuse.

Performance

❓ Frequently Asked Questions

It returns the character-to-entity mapping array used internally by htmlspecialchars() and htmlentities(). Keys are special characters; values are their HTML entity strings.
array get_html_translation_table(int $table = HTML_SPECIALCHARS, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, string $encoding = 'UTF-8'). The $table argument accepts HTML_SPECIALCHARS or HTML_ENTITIES.
HTML_SPECIALCHARS returns a small table for &, <, >, quotes, and related characters. HTML_ENTITIES returns a large table covering many symbols and accented letters—everything htmlentities() can encode.
Pass the table to strtr(): strtr($string, get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES)). This produces the same result as htmlspecialchars() with matching flags.
No. Building the table has overhead. Call it once before the loop and reuse the array—especially important when processing many strings.
Use it when you need to inspect, customize, or extend the entity map—for example merging extra character replacements before calling strtr(). For normal output escaping, htmlspecialchars() alone is simpler.

Explore More PHP String Functions

Continue with hebrev(), hebrevc(), and the rest of the string function reference.

Next: hebrev() →

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.

6 people found this page helpful