Developer HTML Entity Encoder / Decoder

HTML Entity Encoder / Decoder

Convert special characters like &, <, and > to HTML entities, or decode entities back to plain text.

How to use this tool

Encode turns the five characters that mean something in HTML into entities, so the text displays as written. Decode does the reverse for any entity, named or numeric, such as &copy;, &#8377; or &#x20B9;.

Worked example

Encoding <p>Tom & Jerry's "Great" Adventure</p> gives:

&lt;p&gt;Tom &amp; Jerry&#39;s &quot;Great&quot; Adventure&lt;/p&gt;

Pasted into a page, that shows the tag as text instead of starting a paragraph, which is how code samples and user comments are displayed safely. The apostrophe becomes &#39; rather than &apos; because the numeric form also works in old HTML 4 pages and email clients.

Two mistakes to watch for

  • Encoding twice. Encoding text that is already encoded turns &amp; into &amp;amp;, and the page then shows "&amp;" to readers. If you see entity names on a live page, something encoded the text twice; decode once to find the original.
  • Decoded &nbsp; isn't a space. It becomes a non-breaking space (U+00A0), which looks identical but fails comparisons such as value === "Pune Maharashtra" and isn't matched by a plain space in a search. Replace it with an ordinary space if the text goes into data rather than a page.

Frequently Asked Questions

Do I need to encode ₹, é or emoji?

Not on a page saved as UTF-8 with <meta charset="utf-8">: there those characters can be written directly. Only &, <, > and quotes inside attribute values have to be encoded, and that is all Encode changes. Numeric entities such as &#8377; for ₹ are useful when the text must pass through a system that mangles non-ASCII characters.

Does encoding protect against script injection everywhere?

Only in the places where HTML is being parsed: between tags and inside quoted attribute values. Text placed inside a <script> block, a style attribute, an event handler such as onclick, or a URL such as href="javascript:..." needs different escaping or validation, so use your framework's escaping for those contexts.

Is this the same as escaping for JavaScript or URLs?

No. JavaScript strings use backslash escapes (\" and \n), and URLs use percent-encoding (a space becomes %20). Applying HTML entities in those places leaves the text wrong: &amp; inside a URL query is read as the five characters "&amp;" by anything that doesn't parse it as HTML first.

Related Tools

Related Blog Posts