OmniFeed Search

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 &apos;. 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:

CharacterBecomes
&&amp;
<&lt;
>&gt;
"&quot;
'&#39;

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 &#233;, and 😀 becomes &#128512;, one entity for the whole emoji rather than one for each of its two UTF-16 halves.

What the decoder reads

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 &copy 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, &lt; becomes &amp;lt;, and a browser shows the literal text &lt; instead of <.

In code

In Python, html.escape(s) escapes the same five characters but writes the apostrophe as &#x27;, 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 &apos; or &#39;?

Write &#39;, because it works in every HTML parser. &apos; is defined in XML and in HTML5 but not in HTML 4, which is why Encode writes &#39;.

What is &nbsp;?

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.

More in encode and decode

Other tools