HTML Entity Encoder
Turn <, >, & and quotes into safe HTML entities — or decode an escaped string back to text.
Why escape at all
HTML reserves five characters: &, <, >, " and '. A literal < inside text is read as the start of a tag, so a blog comment containing <script> becomes executable code instead of visible text — that is exactly how cross-site scripting works.
Escaping converts each reserved character to an entity the parser treats as data. The result still renders identically to a human: <div> displays as <div>.
Named, decimal and hex entities
Three syntaxes produce the same character:
- Named:
&, ,©— readable, but only ~2,000 names exist and browsers historically disagreed on the edge cases. - Decimal:
&, — universally supported. - Hexadecimal:
&, — same thing in base 16, common in generated output.
For characters outside ASCII, numeric entities are the safest choice when you cannot guarantee the destination is UTF-8 — that is the "also escape non-ASCII" option above. Modern pages declare <meta charset="utf-8"> and rarely need it.
Escaping is context-dependent
HTML-escaping protects text nodes and quoted attribute values. It does not protect you inside <script>, inside a style block, in an unquoted attribute, or in a URL's javascript: scheme — each context has its own rules. Likewise, do not try to sanitise rich HTML with a regex: use a maintained sanitiser with an allow-list (DOMPurify) or a templating engine that escapes by default.
The opposite direction
Unescaping is for reading data, not for building pages. Decoding a string and then inserting it into HTML re-introduces the exact risk escaping removed. If a CMS shows & where you expected &, the value was escaped twice somewhere in the pipeline — fix that, rather than stripping entities at render time.
Frequently asked questions
Does escaping prevent XSS?
Should I store escaped or raw text in the database?
What is &nbsp; and do I need it?
white-space: nowrap) for layout instead; in content creates invisible formatting problems.