SnapSum
← Back to Blog
Development3 min read

HTML Entity Encoder/Decoder: Escape Special Characters Correctly

HTML entities let you display reserved characters (like < and &) and symbols that are not on your keyboard. An encoder/decoder converts between raw text and HTML-entity-escaped text instantly.

What Are HTML Entities?

HTML entities start with & and end with ;. They serve two purposes:

  • Display reserved characters - &lt; displays <, &amp; displays &
  • Display symbols - &copy; → ©, &reg; → ®, &euro; → €
  • Handle non-ASCII characters - &#228; represents ä

When to Encode HTML Entities

  • Dynamic HTML rendering - if you insert user-generated content into innerHTML, encode < and & to prevent XSS attacks.
  • XML feeds - RSS and Atom feeds require entities for reserved characters.
  • Email HTML - some email clients handle raw characters inconsistently; entities are safer.
  • Displaying code snippets - show <div> on a webpage without the browser interpreting it as HTML.

Common HTML Entities Cheat Sheet

  • &lt; - less-than sign <
  • &gt; - greater-than sign >
  • &amp; - ampersand &
  • &quot; - double quote "
  • &apos; - single quote ' (apostrophe)
  • &nbsp; - non-breaking space
  • &copy; - copyright ©
  • &reg; - registered ®
  • &mdash; - em dash -
  • &hellip; - ellipsis …

Entity vs Numeric Code

HTML entities come in two forms:

  • Named entities - &copy;, &euro;. Easier to remember.
  • Numeric entities - &#169; (copyright), &#8364; (euro). More universal, works for any Unicode character.

The SnapSum HTML Entity tool converts between both forms and raw text.

Frequently Asked Questions

Should I encode all user input?

Encode before inserting into innerHTML or rendering in an HTML context. For modern frameworks (React, Vue), the framework handles escaping automatically when using text content or JSX expressions.

What is the difference between &nbsp; and a regular space?

&nbsp; is a non-breaking space - the browser will not wrap a line at that point. Use it to keep "100 km" or "Dr. Smith" on the same line.