htmlentities
htmlentities(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): string
把所有具有 HTML 实体等价的字符全部转为实体(比 htmlspecialchars 覆盖面更广)。
C C23
#include <stdio.h>
#include <string.h>
// 手写版演示核心差异:& < > 最小转义 + 常见 UTF-8 符号转命名实体
int main(void) {
const char *s = "Tom & Jerry — €5";
for (const char *p = s; *p;) {
if (*p == '&') { printf("&"); p++; }
else if (*p == '<') { printf("<"); p++; }
else if (*p == '>') { printf(">"); p++; }
else if (memcmp(p, "—", 3) == 0) { printf("—"); p += 3; }
else if (memcmp(p, "€", 3) == 0) { printf("€"); p += 3; }
else { putchar(*p); p++; }
}
printf("\n");
return 0;
}
Tom & Jerry — €5
C++ C++26
#include <iostream>
#include <string>
// 手写版:& < > 最小转义 + 常见 UTF-8 符号转命名实体
std::string escape_entities(const std::string &s) {
std::string out;
for (size_t i = 0; i < s.size();) {
if (s[i] == '&') { out += "&"; i++; }
else if (s[i] == '<') { out += "<"; i++; }
else if (s[i] == '>') { out += ">"; i++; }
else if (s.compare(i, 3, "\xE2\x80\x94") == 0) { out += "—"; i += 3; }
else if (s.compare(i, 3, "\xE2\x82\xAC") == 0) { out += "€"; i += 3; }
else { out += s[i]; i++; }
}
return out;
}
int main() {
std::cout << escape_entities("Tom & Jerry — €5") << "\n";
return 0;
}
Tom & Jerry — €5
C# v11.0.100
using System.Net;
using System.Text.Encodings.Web;
string s = "Tom & Jerry — €5";
Console.WriteLine(WebUtility.HtmlEncode(s)); // 最小转义:非 ASCII 符号原样保留
Console.WriteLine(HtmlEncoder.Default.Encode(s)); // 全量转义:非 ASCII 转数值实体(近似 htmlentities)
Tom & Jerry — €5 Tom & Jerry — €5
Go v1.27.4
package main
import (
"fmt"
"html"
)
func main() {
s := "Tom & Jerry — €5"
// 标准库只转 5 个敏感字符,非 ASCII 符号保持原样(无 htmlentities 等价物)
fmt.Println(html.EscapeString(s))
}
Tom & Jerry — €5
Java v27.0.1
public class Main {
// 与 htmlspecialchars 条目同一 replace 链;非 ASCII 符号保持原样
static String escapeHtml(String s) {
return s.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace("\"", """)
.replace("'", "'");
}
public static void main(String[] args) {
System.out.println(escapeHtml("Tom & Jerry — €5"));
}
}
Tom & Jerry — €5
JavaScript v26.13.1
// 最小转义不动非 ASCII 符号;全量实体(—/€)需自行查表替换
const escapeHtml = (s) => s
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">");
console.log(escapeHtml("Tom & Jerry — €5"));
Tom & Jerry — €5
PHP v8.6.0
$s = 'Tom & Jerry — €5';
echo htmlspecialchars($s), "\n", htmlentities($s); // 敏感字符集 vs 全量实体表
Tom & Jerry — €5 Tom & Jerry — €5
Python v3.15.2
from html import escape
print(escape("Tom & Jerry — €5")) # 标准库只有最小转义,非 ASCII 原样保留
Tom & Jerry — €5
Rust v1.99.1
// 手写最小转义;非 ASCII 符号(— €)保持原样,全量实体需自行查表
fn escape_html(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
}
fn main() {
println!("{}", escape_html("Tom & Jerry — €5"));
}
Tom & Jerry — €5
TypeScript v26.13.1
// 最小转义不动非 ASCII 符号;全量实体(—/€)需自行查表替换
const escapeHtml = (s: string): string => s
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">");
console.log(escapeHtml("Tom & Jerry — €5"));
Tom & Jerry — €5
语言差异说明
C:手写 memcmp 匹配 UTF-8 双字节序列转命名实体(示例);完整实体表有 250+ 项,生产建议只做最小转义。
C++:同 C 思路,compare 匹配 UTF-8 字节序列(示例,用 \xE2 转义写明字节避免源码编码问题)。
C#:两态对照最有教学价值——WebUtility.HtmlEncode 是最小转义(非 ASCII 原样),HtmlEncoder.Default 把非 ASCII 全转十六进制数值实体(近似 htmlentities 的覆盖面,示例第二行)。
Go:html.EscapeString 固定转 5 个字符、不碰非 ASCII(示例注释)——标准库没有 htmlentities 等价物;需要全量实体得自己维护映射表。
Java:与 htmlspecialchars 条目共用 replace 链(示例);JDK 无实体表工具,Apache Commons Text 的 escapeHtml4/escapeHtml3 才提供具名实体全量转义。
JavaScript:与 htmlspecialchars 共用最小转义(示例注释说明);全量具名实体(—/€)需要查表实现——HTML5 命名实体表有 2000+ 项。
PHP:核心对照在示例本身——htmlspecialchars 只处理 5 个敏感字符(— € 原样),htmlentities 把所有有具名实体的字符全转(— → —、€ → €);htmlentities 必须传对 $encoding(默认 UTF-8),传错时遇到不匹配字节直接返回空串;安全能力两者相同(XSS 防护靠那 5 个字符),htmlentities 的价值在 legacy 场景(非 UTF-8 编码的邮件/页面需要 ASCII 化)。
Python:html.escape 只有最小转义模式(示例注释)——标准库没有全量实体工具;html.unescape 提供反向解码。
Rust:手写最小转义(示例注释说明全量需查表);同 PHP 语义的第三方 crate 有 html-escape。
TypeScript:同 JavaScript(示例);若需全量实体可引入 entities npm 包(HTML5 完整表)。