Skip to content
K Knidox Search…
Computer Science · Encoding

HTML Entity Encode & Decode

Escape characters to HTML entities and decode them back — deterministic, no DOM guesswork.

Direction
Non-ASCII characters
27 chars
Try an example — tap to load
Encoded entities
<a href="x">Tom & Jerry</a>

HTML entities let you show reserved characters as literal text instead of markup. Encoding escapes the five specials: <b> becomes &lt;b&gt;, and & becomes &amp;. Decoding reverses it, turning &#39; and &copy; back into ’ and ©. Encode the ampersand first so entities never double-escape.

What HTML entities are

In HTML a few characters carry structural meaning — & starts an entity, < and > delimit tags, and quotes wrap attribute values. To display those characters as plain text rather than have the browser interpret them, you replace each with an entity: a code that begins with & and ends with a semicolon. Entities come in two flavours: named (like &copy; for ©) and numeric (like &#169; or the hexadecimal &#xA9;). Both resolve to the same character.

character → &name; or &#N; or &#xHEX;

Named uses a keyword; numeric uses the Unicode code point in decimal or hex.

The five that always need escaping

The core set is & < > " and '. The ampersand must be encoded first — if you replaced < with &lt; before escaping &, that new ampersand would itself get turned into &amp;lt;, corrupting the output. Encoding character by character (and never re-scanning what you just wrote) avoids that entirely. Everything else on the page is optional: you only need to encode non-ASCII characters like é or © when your file encoding cannot store them directly.

  1. 1
    Choose Encode. The Encode segment escapes reserved characters in your text.
  2. 2
    Escape the ampersand first. Every & becomes &amp; before anything else, so no entity gets escaped twice.
  3. 3
    Escape the angle brackets and quotes. < → &lt;, > → &gt;, " → &quot;, and ’ → &#39;.
  4. 4
    Optionally encode non-ASCII. Switch “Non-ASCII characters” to Numeric entities and é becomes &#233;.
  5. 5
    Copy the result. <a href="x">Tom & Jerry</a> becomes &lt;a href=&quot;x&quot;&gt;Tom &amp; Jerry&lt;/a&gt;.

Common HTML entities

The five specials plus two everyday extras. Numeric forms work everywhere; named forms are easier to read.

CharacterNamed entityNumeric entityPurpose
&&amp;amp;&amp;#38;Starts every entity — escape it first
<&amp;lt;&amp;#60;Opens a tag
>&amp;gt;&amp;#62;Closes a tag
"&amp;quot;&amp;#34;Wraps attribute values
'&amp;#39;&amp;#39;Apostrophe (&amp;apos; is XML, not classic HTML)
 &amp;nbsp;&amp;#160;Non-breaking space
©&amp;copy;&amp;#169;Copyright sign

Why encode, and named vs numeric

Encoding does two jobs. First, it prevents markup injection: user text containing <script> is rendered as harmless literal characters instead of running as code. Second, it lets you display reserved or unusual characters exactly as written. Choose named entities when readability matters — &copy; is clearer than &#169;. Choose numeric entities for anything without a well-known name, or when you want output that works in strict XML, which recognises only five named entities. Decoding here is pure string work — a named-entity lookup plus a regex for the numeric forms — so it never depends on a browser DOM and always gives the same result.

What is the difference between named and numeric entities?
A named entity uses a keyword, like &copy; for ©, while a numeric entity uses the character’s Unicode code point, like &#169; (decimal) or &#xA9; (hex). Both decode to the same character; named forms read better, numeric forms cover every character.
Why must the ampersand be encoded first?
Because & begins every entity. If you escaped < to &lt; before handling &, the ampersand in &lt; would then be turned into &amp;, producing &amp;lt; and breaking the output. Encoding & first (and never re-scanning) prevents this double-escaping.
Should I use &#39; or &apos; for an apostrophe?
Use &#39;. The named entity &apos; is defined in XML and XHTML but is not part of classic HTML 4, so older parsers may show it literally. The numeric &#39; is understood everywhere, which is why this tool emits it when encoding.
Do I need to escape > (the greater-than sign)?
Strictly, only &, <, and quotes are required. But escaping > to &gt; is a long-standing convention that avoids edge cases with the accidental sequence ]]> and keeps output consistent, so this tool encodes it too.
When should I encode non-ASCII characters like é or ☕?
Only when your document’s encoding cannot store them directly — for example a file saved as ASCII or Latin-1. In a UTF-8 page you can write é as-is. The Numeric entities option is there for those legacy or ASCII-only situations.
Does this decode entities with a browser DOM?
No. Decoding uses a fixed named-entity map plus a regular expression for &#NNN; and &#xHEX; forms, so it is deterministic and does not run any markup. Unknown entities are left untouched rather than guessed at.