OnlineTextLab
Esc

HTML Entity Encoder / Decoder

Escape text so it can sit safely inside HTML, or turn entities like é and – back into the characters they stand for.

Direction
Characters to encode
Result

The result appears here as you type.

Runs on your device. Your files and text are not uploaded.

About HTML Entity Encoder Decoder

A browser treats < as the start of a tag and & as the start of a character reference. To show those characters literally, as you must when publishing a code sample or a sentence about HTML, they need writing as entities. Encoding does that. Decoding is the reverse, for when you pull a string from a feed, a database or a page source and it reads Caf&eacute; &amp; bar.

Direction sits at the top of the page, since it is the one decision the tool needs from you. In encode mode, a second control sets how far it goes: only the characters HTML reserves, or every non-ASCII character as well, for systems that damage UTF-8 in transit.

Double encoding is the most common mistake with this kind of text, and it produces &amp;amp; on a live page. If you paste text into encode mode that already holds entities, a notice suggests switching to decode before you copy anything.

How it works

Encoding replaces five characters: &, <, >, " and '. The ampersand goes first, so the entities being written are not themselves encoded again. The apostrophe becomes &#39; rather than &apos;, because HTML 4 did not define &apos; and the numeric form works everywhere. Everything non-ASCII too then rewrites each remaining character as a decimal reference by full Unicode code point, so an emoji becomes one reference, not two broken halves.

Decoding makes one pass over the text, which matters more than it sounds. &amp;lt; becomes &lt; and stops there, as a browser would do, and does not continue on to <. Hexadecimal and decimal references work up to the top of Unicode. Following the HTML standard, an invalid value such as &#x110000; becomes the replacement character (U+FFFD), and values from 128 to 159 are read as Windows-1252, so the common &#150; from old documents becomes an en dash.

The output pane always shows the result as text and never parses it. Decoding &lt;script&gt; displays the word and runs nothing.

How to use HTML Entity Encoder Decoder

  1. Choose Encode or Decode under Direction.
  2. When encoding, choose Reserved only or Everything non-ASCII too.
  3. Paste your text, use Open file, or press Try an example, which gives a sample suited to the direction you picked.
  4. Press Copy result, or use Download .txt.

Decoding, where the double-encoded ampersand decodes only once

Input
&lt;b&gt;caf&eacute; &amp;amp; bar &#8211; &#x1F319;&lt;/b&gt;
Output
<b>café &amp; bar – 🌙</b>

Named, decimal and hexadecimal references all resolved. The &amp;amp; lost one layer and became &amp;, so run it again if the text really was encoded twice. Encoding Tom's gives Tom&#39;s, and with the default setting café keeps its accent as is.

When it helps

  • Preparing a code sample for a blog post, so <div> shows as text inside a <pre> block.
  • Reading a value from an RSS feed or an API response that arrives full of entities.
  • Cleaning &amp;amp; out of text that a CMS has encoded twice, by decoding until it reads correctly.
  • Writing non-ASCII text into a legacy template or an email system that corrupts UTF-8.

Tips for a better result

  • Encode only the text and not the page around it. To put a value into an attribute or a paragraph, encode that value alone, then place it in your HTML.
  • Stay with Reserved only unless something is actually corrupting your characters. A page served as UTF-8 displays é correctly, and the source stays much easier to read.
  • A decoded &nbsp; is a real non-breaking space (U+00A0), which looks like a space but will not match one in a search. For ordinary spaces, follow up with Find and Replace.

Good to know

  • Names are limited to the 253 from HTML 4, plus &apos; and the HTML5 names for ASCII punctuation such as &colon; and &lpar;. Rarer HTML5 names are left unchanged, though the numeric form of any character always works.
  • A semicolon is required. Browsers accept a few legacy forms like &amp without one, but they are ambiguous in ordinary text, so they are left alone.
  • Encoding makes text safe in HTML element content and quoted attributes. It does not make a value safe inside a <script>, a style block or a URL, which have their own rules.
  • Non-ASCII output uses decimal references only, not names such as &eacute;.
  • This is not URL encoding. For %20 style escaping, use the URL Encoder and Decoder.

Questions people ask

Which characters must be encoded in HTML?

In text, "&" and "<" are the essential two, and ">" by convention. Inside a quoted attribute, the matching quote must be encoded as well. This tool encodes all five reserved characters, so the result is safe in either place without you having to think about which one you are in.

Why do I see &amp;amp; on my page?

The text was encoded twice. The first pass made "&amp;", and the second encoded its ampersand again. Decode it once here to get back one layer, then find which step in your publishing process is encoding text that is already encoded.

Does encoding text stop cross-site scripting?

It is the correct defense when untrusted text goes into HTML content or a quoted attribute, and only there. It is no help inside scripts, style blocks or "href" values, where a "javascript:" address contains nothing to encode. Use your framework's context-aware escaping in real applications.

What to use next

  • Text to HTML: if you also need paragraphs and line breaks, not only escaped characters
  • HTML to Text: to remove the tags as well as decode the entities
  • URL Encode Decode: when the string belongs in a URL rather than in HTML

Last reviewed . Read our editorial policy.