htmlentities

htmlentities(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): string

Converts every character with an HTML entity equivalent to that entity (broader coverage than htmlspecialchars).

C C23

C23
#include <stdio.h>
#include <string.h>

// hand-rolled demo of the core difference: minimal escaping for & < > plus named entities for common UTF-8 symbols
int main(void) {
    const char *s = "Tom & Jerry — €5";
    for (const char *p = s; *p;) {
        if (*p == '&') { printf("&amp;"); p++; }
        else if (*p == '<') { printf("&lt;"); p++; }
        else if (*p == '>') { printf("&gt;"); p++; }
        else if (memcmp(p, "—", 3) == 0) { printf("&mdash;"); p += 3; }
        else if (memcmp(p, "€", 3) == 0) { printf("&euro;"); p += 3; }
        else { putchar(*p); p++; }
    }
    printf("\n");
    return 0;
}
Output
Tom &amp; Jerry &mdash; &euro;5

C++ C++26

C++26
#include <iostream>
#include <string>

// hand-rolled: minimal escaping for & < > plus named entities for common UTF-8 symbols
std::string escape_entities(const std::string &s) {
    std::string out;
    for (size_t i = 0; i < s.size();) {
        if (s[i] == '&') { out += "&amp;"; i++; }
        else if (s[i] == '<') { out += "&lt;"; i++; }
        else if (s[i] == '>') { out += "&gt;"; i++; }
        else if (s.compare(i, 3, "\xE2\x80\x94") == 0) { out += "&mdash;"; i += 3; }
        else if (s.compare(i, 3, "\xE2\x82\xAC") == 0) { out += "&euro;"; i += 3; }
        else { out += s[i]; i++; }
    }
    return out;
}

int main() {
    std::cout << escape_entities("Tom & Jerry — €5") << "\n";
    return 0;
}
Output
Tom &amp; Jerry &mdash; &euro;5

C# v11.0.100

v11.0.100
using System.Net;
using System.Text.Encodings.Web;

string s = "Tom & Jerry — €5";
Console.WriteLine(WebUtility.HtmlEncode(s)); // minimal escaping: non-ASCII symbols stay as-is
Console.WriteLine(HtmlEncoder.Default.Encode(s)); // full escaping: non-ASCII to numeric entities (close to htmlentities)
Output
Tom &amp; Jerry — €5
Tom &amp; Jerry &#x2014; &#x20AC;5

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"html"
)

func main() {
	s := "Tom & Jerry — €5"
	// the standard library escapes only 5 sensitive characters and keeps non-ASCII as-is (no htmlentities equivalent)
	fmt.Println(html.EscapeString(s))
}
Output
Tom &amp; Jerry — €5

Java v27.0.1

v27.0.1
public class Main {
    // same replace chain as the htmlspecialchars entry; non-ASCII symbols stay as-is
    static String escapeHtml(String s) {
        return s.replace("&", "&amp;")
                .replace("<", "&lt;")
                .replace(">", "&gt;")
                .replace("\"", "&quot;")
                .replace("'", "&#039;");
    }

    public static void main(String[] args) {
        System.out.println(escapeHtml("Tom & Jerry — €5"));
    }
}
Output
Tom &amp; Jerry — €5

JavaScript v26.13.1

v26.13.1
// minimal escaping leaves non-ASCII alone; full entities (&mdash;/&euro;) need a lookup table
const escapeHtml = (s) => s
  .replaceAll("&", "&amp;")
  .replaceAll("<", "&lt;")
  .replaceAll(">", "&gt;");
console.log(escapeHtml("Tom & Jerry — €5"));
Output
Tom &amp; Jerry — €5

PHP v8.6.0

v8.6.0
$s = 'Tom & Jerry — €5';
echo htmlspecialchars($s), "\n", htmlentities($s); // sensitive-char set vs full entity table
Output
Tom &amp; Jerry — €5
Tom &amp; Jerry &mdash; &euro;5

Python v3.15.2

v3.15.2
from html import escape
print(escape("Tom & Jerry — €5"))  # the standard library only does minimal escaping; non-ASCII stays as-is
Output
Tom &amp; Jerry — €5

Rust v1.99.1

v1.99.1
// hand-written minimal escaping; non-ASCII symbols (— €) stay as-is, full entities need a table
fn escape_html(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

fn main() {
    println!("{}", escape_html("Tom & Jerry — €5"));
}
Output
Tom &amp; Jerry — €5

TypeScript v26.13.1

v26.13.1
// minimal escaping leaves non-ASCII alone; full entities (&mdash;/&euro;) need a lookup table
const escapeHtml = (s: string): string => s
  .replaceAll("&", "&amp;")
  .replaceAll("<", "&lt;")
  .replaceAll(">", "&gt;");
console.log(escapeHtml("Tom & Jerry — €5"));
Output
Tom &amp; Jerry — €5

Language differences

C: hand-written memcmp matching two-byte UTF-8 sequences for named entities (example); the full entity table has 250+ entries — minimal escaping is the pragmatic production choice.

C++: same approach as C with compare matching UTF-8 byte sequences (example, using \xE2 escapes to make bytes explicit and avoid source-encoding issues).

C#: the two-mode contrast is the most instructive — WebUtility.HtmlEncode is minimal (non-ASCII untouched), while HtmlEncoder.Default turns all non-ASCII into hexadecimal numeric entities (close to htmlentities' coverage, second line of the example).

Go: html.EscapeString always escapes exactly 5 characters and leaves non-ASCII intact (see the example comment) — the standard library has no htmlentities equivalent; full entity tables must be hand-maintained.

Java: shares the replace chain with the htmlspecialchars entry (example); the JDK ships no entity table — Apache Commons Text's escapeHtml4/escapeHtml3 provide full named-entity escaping.

JavaScript: shares the minimal escaping with htmlspecialchars (see the example comment); full named entities (&mdash;/&euro;) require a lookup-table implementation — the HTML5 named entity table has 2000+ entries.

PHP: the core contrast is in the example itself — htmlspecialchars handles only the 5 sensitive characters (— € pass through), while htmlentities converts every character that has a named entity (— → &mdash;, € → &euro;); htmlentities requires a correct $encoding (default UTF-8) and returns an empty string on mismatched bytes; security-wise both are equivalent (XSS protection rests on those 5 characters), and htmlentities' niche is legacy scenarios (non-UTF-8 email/pages needing ASCII-only output).

Python: html.escape only has the minimal mode (see the example comment) — no full-entity tool in the standard library; html.unescape covers the reverse direction.

Rust: hand-written minimal escaping (the example comment notes a table is needed for full coverage); the html-escape crate mirrors PHP semantics.

TypeScript: same as JavaScript (example); for full entity tables the entities npm package (complete HTML5 table) is the usual pick.