HTML entity encoder / decoder
Encode escapes &, <, >, ", and ' so your text is safe to put inside HTML, and Decode turns entities back into characters. Decode knows every numeric entity, the 252 named entities from HTML 4, and '. It leaves any other name as it is and lists it below the result. It runs in your browser; nothing you enter is uploaded.
Which characters need escaping
In HTML text, two characters can break the markup: <, which starts a tag, and &, which starts an entity.
Inside an attribute value, the quote that wraps the value has to be escaped too, or it ends the value early. Encode escapes five characters
wherever they appear, so its output is safe in text and in attributes quoted with either kind of quote:
| Character | Becomes |
|---|---|
& | & |
< | < |
> | > |
" | " |
' | ' |
HTML doesn't require > to be escaped. Encode escapes it anyway, as Python's html.escape and PHP's
htmlspecialchars do, because the result reads more clearly as markup.
A web page served as UTF-8 can hold é or 😀 as they are, so most sites don't need entities for them. Tick the non-ASCII option
when the text is going somewhere that accepts only ASCII, such as an old template system or a source file whose encoding you can't control.
Encode then writes each of those characters as its decimal code point: é becomes é, and 😀 becomes
😀, one entity for the whole emoji rather than one for each of its two UTF-16 halves.
What the decoder reads
-
Numeric entities for any Unicode character, in decimal (
é) or hex (é). For the odd cases, the decoder follows the HTML standard:�, surrogate code points, and numbers above 10FFFF hex become the replacement character U+FFFD. The numbers 128 to 159 become the Windows-1252 characters old pages meant by them, so–is an en dash. -
The 252 named entities defined in HTML 4, plus
'. These cover the Latin-1 letters and signs such aséand©, typographic punctuation such as—and…, the Greek letters, arrows, and common math symbols.
The current HTML standard defines over 2,000 names, listed in its
named character references table, and the
decoder doesn't know most of them. A name it doesn't know, such as &Hearts;, stays in the output as you typed it, and the
decoder lists it under the result. The decoder also needs every entity's closing semicolon. Browsers still accept a few legacy names
without one, such as © in old pages, but the decoder leaves those unchanged.
Where escaping isn't enough
Entities make text safe in HTML text and in quoted attribute values. They don't protect a value you put inside a
<script> block, a style attribute, or a URL in href; each of those needs its own escaping. A
javascript: link, for example, passes through HTML escaping unchanged and still runs when someone clicks it. The
OWASP cross-site scripting prevention cheat sheet gives the rule for each of these places.
Encode text only once. If you run Encode on text that's already escaped, < becomes &lt;, and a
browser shows the literal text < instead of <.
In code
In Python, html.escape(s) escapes the same five characters but writes the apostrophe as ', and
html.unescape(s) decodes every name in the HTML standard's list. In PHP, use htmlspecialchars and
html_entity_decode.
JavaScript has no built-in function for either. In a browser, though, setting an element's textContent inserts text without
treating it as markup, and new DOMParser().parseFromString(s, 'text/html').documentElement.textContent decodes every entity
the browser knows.
Questions
Should I write ' or '?
Write ', because it works in every HTML parser. ' is defined in XML and in HTML5 but not in
HTML 4, which is why Encode writes '.
What is ?
It's a no-break space, U+00A0. It looks like a space, but a line never breaks at it, and HTML doesn't collapse it into the spaces next to it. Decode turns it into that character rather than an ordinary space, so a search for a normal space won't find it afterward.