DevKit

HTML Entity Encoder

Turn <, >, & and quotes into safe HTML entities — or decode an escaped string back to text.

Free No sign-up Runs in your browser
Raw text
Escaped HTML

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: &lt;div&gt; displays as <div>.

Named, decimal and hex entities

Three syntaxes produce the same character:

  • Named: &amp;, &nbsp;, &copy; — readable, but only ~2,000 names exist and browsers historically disagreed on the edge cases.
  • Decimal: &#38;, &#160; — universally supported.
  • Hexadecimal: &#x26;, &#xA0; — 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 &amp; 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?
Only when applied to the right context. Escaping text and quoted attributes is effective; inside script blocks, style blocks, unquoted attributes or URLs it is not enough. Use contextual auto-escaping (React, Vue, Go templates) or a sanitiser for user-supplied HTML.
Should I store escaped or raw text in the database?
Store raw text. Escape at render time, for the specific output context. Pre-escaping in the database guarantees double-escaping bugs and breaks non-HTML consumers such as JSON APIs and search indexes.
What is &amp;nbsp; and do I need it?
It is a non-breaking space — it renders like a space but prevents a line break. Use CSS (white-space: nowrap) for layout instead; &nbsp; in content creates invisible formatting problems.

Related tools