HTML Entity Encoder and Decoder

The five that matter, and nothing else.

Direction

The five that matter, and nothing else.

How to use it

  1. Paste the text. Text to escape, or HTML containing entities to decode.
  2. Pick the direction. Decoding handles named, decimal and hexadecimal entities.
  3. Copy the result. Safe to put into a template or an attribute.

When you would use this

Escaping HTML is five replacements and an ordering constraint, and the ordering is what people get wrong. The ampersand has to be replaced first. Escape the less than sign first and its replacement contains an ampersand, which the ampersand rule then escapes again, and a less than sign comes out as < on the page instead of a less than sign. It is a one line mistake with a visible, confusing result. Only five characters are escaped: ampersand, less than, greater than, and both quote marks. Those are the ones that are structural in HTML and in XML. Encoding accented characters as entities as well is a convention from before UTF-8 was universal, and today it triples the size of anything not written in English and makes the source unreadable for no benefit at all. Decoding handles named entities, decimal references and hexadecimal ones, including code points above the basic plane so emoji come back correctly. An entity it does not recognise is left exactly as written, because guessing corrupts text quietly and leaving it is recoverable.

Questions

Why does it not encode accented characters?
Because there is no reason to. Entity encoding everything non-ASCII is a habit from before UTF-8 was universal, and it roughly triples the size of anything not written in English while making it unreadable in the source. Only the five characters that are structural in HTML need escaping.
Does it double encode?
No. The ampersand is replaced first, which is what stops every entity written afterwards having its own ampersand escaped. Getting that order wrong turns a less than sign into < and is the most common bug in a hand written escaper.
What happens to an entity it does not recognise?
It is left exactly as written. Guessing would corrupt the text silently, and leaving it means you can see what happened and fix it.