html_entity_decode
html_entity_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null): string
Converts HTML entities back to characters — the inverse of htmlentities.
C C23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// No HTML decoding in the standard library: hand-written numeric references + common named entities (libxml2 in real projects)
static void put_utf8(unsigned int cp) {
if (cp < 0x80) {
putchar(cp);
} else if (cp < 0x800) {
putchar(0xC0 | (cp >> 6));
putchar(0x80 | (cp & 0x3F));
} else {
putchar(0xE0 | (cp >> 12));
putchar(0x80 | ((cp >> 6) & 0x3F));
putchar(0x80 | (cp & 0x3F));
}
}
int main(void) {
const char *s = "<p> Tom & Jerry © AB";
while (*s) {
if (*s == '&') {
const char *semi = strchr(s, ';');
size_t len = semi ? (size_t)(semi - s - 1) : 0;
if (semi && len >= 1 && len <= 10) {
const char *body = s + 1;
if (body[0] == '#') {
put_utf8((unsigned int)strtoul(body + 1, NULL, 10));
s = semi + 1;
continue;
}
if (len == 2 && !strncmp(body, "lt", 2)) { putchar('<'); s = semi + 1; continue; }
if (len == 2 && !strncmp(body, "gt", 2)) { putchar('>'); s = semi + 1; continue; }
if (len == 3 && !strncmp(body, "amp", 3)) { putchar('&'); s = semi + 1; continue; }
if (len == 4 && !strncmp(body, "quot", 4)) { putchar('"'); s = semi + 1; continue; }
if (len == 4 && !strncmp(body, "copy", 4)) { fputs("©", stdout); s = semi + 1; continue; }
}
}
putchar(*s++);
}
putchar('\n');
return 0;
}
<p> Tom & Jerry © AB
C++ C++26
#include <cstdlib>
#include <iostream>
#include <string>
// No HTML decoding in the standard library: hand-written numeric references + common named entities (html-escape/libxml2 in real projects)
static void put_utf8(unsigned int cp, std::string &out) {
if (cp < 0x80) {
out += (char)cp;
} else if (cp < 0x800) {
out += (char)(0xC0 | (cp >> 6));
out += (char)(0x80 | (cp & 0x3F));
} else {
out += (char)(0xE0 | (cp >> 12));
out += (char)(0x80 | ((cp >> 6) & 0x3F));
out += (char)(0x80 | (cp & 0x3F));
}
}
int main() {
std::string s = "<p> Tom & Jerry © AB";
std::string out;
size_t i = 0;
while (i < s.size()) {
if (s[i] == '&') {
size_t semi = s.find(';', i);
if (semi != std::string::npos && semi - i >= 2 && semi - i <= 11) {
std::string body = s.substr(i + 1, semi - i - 1);
if (body[0] == '#') {
put_utf8((unsigned int)strtoul(body.c_str() + 1, nullptr, 10), out);
i = semi + 1;
continue;
}
if (body == "lt") { out += '<'; i = semi + 1; continue; }
if (body == "gt") { out += '>'; i = semi + 1; continue; }
if (body == "amp") { out += '&'; i = semi + 1; continue; }
if (body == "quot") { out += '"'; i = semi + 1; continue; }
if (body == "copy") { out += "©"; i = semi + 1; continue; }
}
}
out += s[i++];
}
std::cout << out << "\n";
return 0;
}
<p> Tom & Jerry © AB
C# v11.0.100
using System.Net;
// WebUtility.HtmlDecode: full decoding in the standard library (named + numeric references)
Console.WriteLine(WebUtility.HtmlDecode("<p> Tom & Jerry © AB"));
<p> Tom & Jerry © AB
Go v1.27.4
package main
import (
"fmt"
"html"
)
func main() {
// html.UnescapeString: full decoding of named + numeric references (EscapeString escapes only 5 basic characters)
fmt.Println(html.UnescapeString("<p> Tom & Jerry © AB"))
}
<p> Tom & Jerry © AB
Java v27.0.1
import java.util.Map;
public class Main {
// No HTML decoding API in the standard library (commons-text's StringEscapeUtils is the idiomatic supplement): hand-written numeric references + common named entities
static String decode(String s) {
Map<String, String> named = Map.of(
"lt", "<", "gt", ">", "amp", "&", "quot", "\"", "copy", "©");
StringBuilder out = new StringBuilder();
for (int i = 0; i < s.length(); ) {
char c = s.charAt(i);
if (c == '&' && s.indexOf(';', i) > i) {
int semi = s.indexOf(';', i);
String body = s.substring(i + 1, semi);
if (body.matches("#\\d+")) {
out.appendCodePoint(Integer.parseInt(body.substring(1)));
i = semi + 1;
continue;
}
String rep = named.get(body);
if (rep != null) {
out.append(rep);
i = semi + 1;
continue;
}
}
out.append(c);
i++;
}
return out.toString();
}
public static void main(String[] args) {
System.out.println(decode("<p> Tom & Jerry © AB"));
}
}
<p> Tom & Jerry © AB
JavaScript v26.13.1
// JS has no built-in entity decoding: hand-written numeric references + common named entities; & must be replaced last to prevent double decoding
const decode = (s) =>
s
.replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(Number(n)))
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, '"')
.replace(/©/g, "©")
.replace(/&/g, "&");
console.log(decode("<p> Tom & Jerry © AB"));
<p> Tom & Jerry © AB
PHP v8.6.0
// The inverse of htmlentities: decodes both named entities (&) and numeric references (A)
$encoded = htmlentities("<p> Tom & Jerry © AB");
echo $encoded, "\n";
echo html_entity_decode($encoded);
<p> Tom & Jerry © AB <p> Tom & Jerry © AB
Python v3.15.2
import html
# html.unescape: full decoding of named + numeric references (html.escape escapes only 5 basic characters — an asymmetry)
encoded = html.escape("<p> Tom & Jerry © AB")
print(encoded)
print(html.unescape(encoded))
<p> Tom & Jerry © AB <p> Tom & Jerry © AB
Rust v1.99.1
// No HTML decoding in the standard library: the html-escape crate is idiomatic; this demo hand-writes numeric references + common named entities
fn decode(s: &str) -> String {
let named: &[(&str, &str)] = &[
("lt", "<"),
("gt", ">"),
("quot", "\""),
("copy", "©"),
("amp", "&"), // & goes last to prevent double decoding
];
let mut out = String::new();
let bytes = s.as_bytes();
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'&' {
if let Some(semi) = s[i..].find(';') {
let body = &s[i + 1..i + semi];
if let Some(rest) = body.strip_prefix('#') {
if let Ok(cp) = rest.parse::<u32>() {
if let Some(ch) = char::from_u32(cp) {
out.push(ch);
i += semi + 1;
continue;
}
}
}
if let Some((_, rep)) = named.iter().find(|(k, _)| *k == body) {
out.push_str(rep);
i += semi + 1;
continue;
}
}
}
let ch = s[i..].chars().next().unwrap();
out.push(ch);
i += ch.len_utf8();
}
out
}
fn main() {
println!("{}", decode("<p> Tom & Jerry © AB"));
}
<p> Tom & Jerry © AB
TypeScript v26.13.1
// no built-in entity decoding: hand-written map; & must be replaced last
const decode = (s: string): string =>
s
.replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(Number(n)))
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, '"')
.replace(/©/g, "©")
.replace(/&/g, "&");
console.log(decode("<p> Tom & Jerry © AB"));
<p> Tom & Jerry © AB
Language differences
C: no standard decoder — hand-write numeric references plus a named-entity table (shown); for real work use libxml2 (htmlEntityLookup).
C++: same hand-rolled approach as C; html-escape (single header) or libxml2 wrappers for real work.
C#: System.Net.WebUtility.HtmlDecode decodes fully in the standard library (HttpUtility.HtmlDecode is the older web-scenario entry).
Go: html.UnescapeString decodes everything in the standard library; note html.EscapeString only escapes 5 basic characters (<, >, &, ', ") — asymmetric with decoding.
Java: no dedicated standard API — Apache Commons Text StringEscapeUtils.unescapeHtml4 is the de facto standard; the example hand-writes a minimal decoder.
JavaScript: nothing built into the language — browsers can use the DOM trick (assign to textarea.innerHTML, read .value), Node ecosystems favor the he library; the example hand-writes a minimal map (& must be replaced last).
PHP: html_entity_decode is the inverse of htmlentities — flags control quote style (ENT_QUOTES/ENT_COMPAT) and document type (ENT_HTML401/ENT_XML1/ENT_XHTML/ENT_HTML5); keep flags and encoding consistent for round trips.
Python: html.unescape decodes everything (including the HTML5 entity table); html.escape only escapes the 5 basic characters, so the pair is not fully symmetric.
Rust: nothing in std — the html-escape crate is the idiom; the example hand-writes a minimal decoder.
TypeScript: same as JavaScript; prefer the DOM trick in browsers.