HTML entity encoder and decoder

Escape text as HTML entities, named or numeric, or decode entities back to characters.

Input

The text to escape, or text with entities to decode.
Encode characters as entities, or decode entities to characters.
How to write entities: named where a name exists (é), decimal (é) or hex (é).
What to encode: "special" is only & < > " and ', "non-ascii" is only characters outside plain ASCII, "all" is both.

Result

Fill in the input and press Run.

Your input is processed in memory and then discarded. It is not stored or logged. Even so, don't paste live passwords or keys into any online tool.

Use it from code or an agent

curl -s https://setget.net/v1/tools/html-entities \
  -H 'Content-Type: application/json' \
  -d '{"input":{"text":"<p>Café & \"tea\"</p>"}}'
import json, urllib.request

body = {'input': {'text': '<p>Café & "tea"</p>'}}
request = urllib.request.Request("https://setget.net/v1/tools/html-entities", json.dumps(body).encode(),
                                 {"Content-Type": "application/json"})
print(json.load(urllib.request.urlopen(request))["result"])
const response = await fetch("https://setget.net/v1/tools/html-entities", {
  method: "POST",
  headers: {"Content-Type": "application/json"},
  body: JSON.stringify({"input":{"text":"<p>Café & \"tea\"</p>"}}),
});
console.log((await response.json()).result);
Tool: run_tool
Arguments:
{
  "slug": "html-entities",
  "input": {
    "text": "<p>Café & \"tea\"</p>"
  }
}
{
  "input": {
    "text": "<p>Café & \"tea\"</p>"
  }
}

Endpoint: POST /v1/tools/html-entities. Over MCP: run_tool with slug: "html-entities". Machine-readable: input schema, Markdown. Connect an agent.

Inputs

NameTypeRequiredWhat it does
text string yes The text to escape, or text with entities to decode.
action string no Encode characters as entities, or decode entities to characters. One of: encode, decode. Default: "encode".
style string no How to write entities: named where a name exists (&eacute;), decimal (&#233;) or hex (&#xE9;). One of: named, decimal, hex. Default: "named".
scope string no What to encode: "special" is only & < > " and ', "non-ascii" is only characters outside plain ASCII, "all" is both. One of: all, special, non-ascii. Default: "all".

Examples

Input
{
  "text": "<p>Café & \"tea\"</p>"
}
Result
{
  "text": "&lt;p&gt;Caf&eacute; &amp; &quot;tea&quot;&lt;/p&gt;",
  "replaced": 8
}

Only non-ASCII characters, leaving the markup alone

Input
{
  "text": "Café <b>€5</b>",
  "style": "decimal",
  "scope": "non-ascii"
}
Result
{
  "text": "Caf&#233; <b>&#8364;5</b>",
  "replaced": 2
}
Input
{
  "text": "&lt;p&gt;Caf&eacute; &amp; &#8364;5 &#x1F600;&lt;/p&gt;",
  "action": "decode"
}
Result
{
  "text": "<p>Café & €5 😀</p>"
}

Questions

Which characters must be escaped in HTML?
Always & and <. Inside attribute values also the quote character. Escaping > and both quotes everywhere, as "special" does, is the safe habit.
Is escaping enough to stop XSS?
Only when the text is placed in HTML content or a quoted attribute. Text placed inside a script, a style or a URL needs different escaping.
Why is the apostrophe written as &#39;?
It works everywhere, while the named form &apos; is not understood by some older HTML readers.