TheFileTools

How to escape and unescape HTML entities

Escape the characters that break HTML so code shows as text, or decode entities back into characters, and where escaping is not enough.

If you paste a code sample into a web page, the browser treats every < as the start of a tag and the sample vanishes or breaks the layout. Escaping replaces the handful of characters that have a meaning in HTML with entities, so they are displayed and not interpreted. The reverse is needed when exported text is littered with things like &amp; and &#39;.

Steps

  1. Open HTML entity encoder.
  2. Choose Encode to escape, or Decode to turn entities back into characters.
  3. Paste the text and copy the result.

What Encode changes

Exactly five characters, and nothing else:

  • & becomes &amp;
  • < becomes &lt;
  • > becomes &gt;
  • the double quote becomes &quot;
  • the single quote becomes &#39;

So <b>Tom & Jerry</b> turns into &lt;b&gt;Tom &amp; Jerry&lt;/b&gt;. Accented letters and emoji are left as they are, which is correct for a page saved as UTF-8.

Encoding text that is already encoded escapes it a second time: &amp; becomes &amp;amp;. If a page shows a visible &amp; where an ampersand should be, the text was escaped twice somewhere. Run Decode once to repair it.

What Decode understands

  • All numeric entities, decimal and hexadecimal: &#169; and &#xA9; both become the copyright sign.
  • A short list of named entities: amp, lt, gt, quot, apos, nbsp, copy, reg, euro, hellip, ndash, mdash, laquo, raquo and trade.

Any other named entity, for example &eacute;, is left in the text unchanged. That is a real limit of this tool: it does not carry the full HTML entity table. Note also that &nbsp; becomes a non-breaking space, which looks like an ordinary space but is a different character.

Escaping and security

These five replacements are what makes text safe between tags and inside a quoted attribute value. They are not sufficient inside a script block, a style block or a URL. Text destined for a link address needs percent-encoding, which URL encoder / decoder does.

To swap one specific piece of text everywhere, use Find and replace.