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.
Fundamentals
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 < or &).
💡
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.
Foundation
📝 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
$table — HTML_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.
Cheat Sheet
⚡ 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 "
Hands-On
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.
Manual (strtr): Tom & Jerry <script>alert("x")</script>
htmlspecialchars: Tom & Jerry <script>alert("x")</script>
Match: yes
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["…"] = "…"; // custom: ellipsis
$table["—"] = "—"; // custom: em dash
$inputs = [
"Wait… what?",
"Range: 1—10",
"Plain & simple",
];
foreach ($inputs as $line) {
echo strtr($line, $table) . "\n";
}
?>
📤 Output:
Wait… what?
Range: 1—10
Plain & simple
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.
Applications
🚀 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.
Important
📝 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.
Wrap Up
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.
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
Summary
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
📝01
Entity Map
Char → entity array.
Purpose
📈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.