get_html_translation_table
get_html_translation_table(int $table = HTML_SPECIALCHARS, int $flags = ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401, string $encoding = "UTF-8"): array
Returns the translation table used internally by htmlspecialchars/htmlentities (character → entity); HTML_SPECIALCHARS holds the five markup characters, HTML_ENTITIES the full set (~1500 entries under HTML5); the default flags include ENT_QUOTES since 8.1.
C C23
#include <stdio.h>
#include <string.h>
/* the C standard library has no HTML entities: hand-roll the five specials */
static void html_escape(const char *s, char *out) {
size_t o = 0;
for (; *s; s++) {
const char *rep = *s == '&' ? "&" : *s == '<' ? "<"
: *s == '>' ? ">" : *s == '"' ? """
: *s == '\'' ? "'" : NULL;
if (rep) { size_t l = strlen(rep); memcpy(out + o, rep, l); o += l; }
else out[o++] = *s;
}
out[o] = '\0';
}
int main(void) {
char buf[64] = {0}; /* zero-initialized output buffer */
printf("& -> &\n");
html_escape("<a&>", buf);
printf("escape=%s\n", buf);
return 0;
}
& -> & escape=<a&>
C++ C++26
#include <iostream>
#include <string>
// no HTML entity table in the C++ standard library: hand-roll the five specials
static std::string html_escape(const std::string &s) {
std::string out;
out.reserve(s.size());
for (char ch : s) {
switch (ch) {
case '&': out += "&"; break;
case '<': out += "<"; break;
case '>': out += ">"; break;
case '"': out += """; break;
case '\'': out += "'"; break;
default: out += ch;
}
}
return out;
}
int main() {
std::cout << "escape=" << html_escape("<a&>") << '\n';
std::cout << "one & grows to 5=" << (html_escape("&").size() == 5) << '\n';
return 0;
}
escape=<a&> one & grows to 5=1
C# v11.0.100
using System;
using System.Collections.Generic;
using System.Net;
var table = new Dictionary<char, string>
{
['&'] = "&",
['<'] = "<",
['>'] = ">",
};
// no public translation-table API: hand-roll the htmlspecialchars-style five
Console.WriteLine($"& -> {table['&']}, < -> {table['<']}, > -> {table['>']}");
Console.WriteLine($"HtmlEncode={WebUtility.HtmlEncode("<a&>")}");
& -> &, < -> <, > -> > HtmlEncode=<a&>
Go v1.27.4
package main
import (
"fmt"
"html"
)
func main() {
// html.EscapeString escapes the five specials; std exposes no editable table
fmt.Println("EscapeString=", html.EscapeString("<a&>"))
fmt.Println("quot=", html.EscapeString("\""), "; apos=", html.EscapeString("'"))
}
EscapeString= <a&> quot= " ; apos= '
Java v27.0.1
import java.util.Map;
public class Main {
// the JDK has no public HTML translation table: hand-roll the five specials
static final Map<Character, String> TABLE = Map.of(
'&', "&", '<', "<", '>', ">", '"', """, '\'', "'");
static String escape(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) sb.append(TABLE.getOrDefault(c, String.valueOf(c)));
return sb.toString();
}
public static void main(String[] args) {
System.out.println("& -> " + TABLE.get('&'));
System.out.println("escape=" + escape("<a&>"));
}
}
& -> & escape=<a&>
JavaScript v26.13.1
// no translation-table API: the replacement map IS the table, in one replace call
function htmlEscape(s) {
return s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
}
console.log("escape=", htmlEscape("<a&>"));
console.log("quot=", htmlEscape('"'), "; apos=", htmlEscape("'"));
escape= <a&> quot= " ; apos= '
PHP v8.6.0
// the table behind htmlspecialchars(): keys are characters, values are
// entities; HTML_SPECIALCHARS holds the five markup specials (ENT_QUOTES
// default since 8.1 adds the single quote).
$table = get_html_translation_table(HTML_SPECIALCHARS);
echo 'count=', count($table), '; ', $table['&'], ',', $table['<'], ',', $table['"'], "\n";
count=5; &,<,"
Python v3.15.2
# html.entities.html5 maps entity names to characters (note: PHP's table is the reverse)
import html
import html.entities
entities = html.entities.html5
print("amp->", entities["amp"], "; lt->", entities["lt"], "; gt->", entities["gt"])
print("escape via html.escape=", html.escape('<script>'))
amp-> & ; lt-> < ; gt-> > escape via html.escape= <script>
Rust v1.99.1
// no HTML entity table in std: hand-roll the htmlspecialchars-style five
fn html_escape(s: &str) -> String {
let mut out = String::new();
for c in s.chars() {
match c {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' => out.push_str("""),
'\'' => out.push_str("'"),
_ => out.push(c),
}
}
out
}
fn main() {
let table = [('&', "&"), ('<', "<"), ('>', ">")];
for (ch, entity) in table {
println!("{ch} -> {entity}");
}
println!("escape={}", html_escape("<a&>"));
}
& -> & < -> < > -> > escape=<a&>
TypeScript v26.13.1
// no translation-table API: the replacement map IS the table, in one replace call
function htmlEscape(s: string): string {
return s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c] as string);
}
console.log("escape=", htmlEscape("<a&>"));
console.log("quot=", htmlEscape('"'), "; apos=", htmlEscape("'"));
escape= <a&> quot= " ; apos= '
Language differences
PHP: get_html_translation_table exposes the mapping used INSIDE htmlspecialchars/htmlentities as an array (keys = characters, values = entities): HTML_SPECIALCHARS is the five XSS-fighting markup characters (&, ", ', <, > — the single quote is in the table since 8.1 because the default flags include ENT_QUOTES), and HTML_ENTITIES holds ~1500 entries under ENT_HTML5 (named entities like 
 and &excl.); the flags mirror htmlspecialchars (ENT_QUOTES/ENT_NOQUOTES for quotes, ENT_HTML401/ENT_XHTML/ENT_HTML5 for the dialect, ENT_SUBSTITUTE for bad-byte replacement) and encoding picks the charset (UTF-8 by default); note one character has several valid entity spellings (" can be "/"/") and the table returns only the one htmlspecialchars actually uses; classic uses: array_flip for a decode table (the manual path beside html_entity_decode), auditing escaping logic, exporting the table to the frontend; per a user note on the manual page, calling it repeatedly inside loops is slow (hoist it out) — pairs with htmlspecialchars (see that entry): use that for application-layer escaping, this table for introspection and custom mappings.
C: no HTML entities whatsoever in the standard library — the example hand-rolls the five-character escape with a zero-initialized output buffer; named entities do not exist as a concept.
C++: hand-written switch escape the same way; out.reserve pre-sizes; the standard library still ignores HTML.
C#: no public table API — WebUtility.HtmlEncode (the example) is the application-layer escape entry (it escapes the five characters); custom tables are hand-built Dictionaries (the example).
Go: html.EscapeString (the example) escapes the five characters but uses NUMERIC entities for quotes ("/', unlike PHP's "/' style — semantically equal, different bytes); the standard library exposes no editable table.
Java: the JDK has no HTML escaping — Commons Text's StringEscapeUtils.escapeHtml4 or hand-rolling; the example builds a five-character Map.of table and scans with getOrDefault.
JavaScript: no table API — the idiom is one replace with a mapping object (the object IS the table in the example); the DOM's entity machinery (textContent vs innerHTML) is covered in the escapehtml entries.
Python: html.entities.html5 (the example) is the direct standard-library match — mind that the DIRECTION is the reverse of PHP (entity name → character), and the html5 table holds 2000+ named entities (HTML5 additions included); application-layer escaping is html.escape; codepoint2name gives a codepoint → name view.
Rust: no HTML entities in std — third-party ammonia/html-escape crates or a hand-written match (the example).
TypeScript: same as JavaScript; the mapping object takes an as string assertion to narrow the indexed access.
Teaching anchor: is an escape function a black box or a white box — PHP is the only language that opens the box into an exportable, editable table, Python's html.entities offers the reverse named-entity universe, and elsewhere the table hides in third-party libraries or reduces to five hand-written lines; a second anchor is entity DIALECT: one double quote has three valid spellings ("/"/") and languages picked different defaults (PHP named, Go numeric) — align the dialect before aligning escape output across languages.