HTML Entity Encoder/Decoder

Convert special characters to and from HTML entities.

Overview

Converts reserved HTML characters into named entity references (&, <, >) and decimal/hex character codes to prevent XSS attacks and rendering bugs.

Formula & Calculation Method

HTML_Entities = Replace_Characters({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;', "'": '&#39;' })

Ensures reserved markup delimiters are rendered literally by web browsers rather than executed as DOM markup.

Worked Example: Escaping Script Tag String

Inputs: Raw String: '<script>alert("XSS");</script>'

  • Replace '<' with '&lt;'.
  • Replace '>' with '&gt;'.
  • Replace '"' with '&quot;'.
  • Result: '&lt;script&gt;alert(&quot;XSS&quot;);&lt;/script&gt;'

Summary: Escaped HTML Entities | XSS Injection Prevented | Browser Displays Literal Text

Common Use Cases

  • XSS Sanitization: Escape untrusted user comments before rendering them into dynamic web pages.
  • Code Block Display: Render literal HTML tags inside <code> and <pre> elements on technical blogs.
  • Special Symbol Rendering: Display copyright symbols (&copy;), currency symbols (&pound;, &yen;), and math characters.

Frequently Asked Questions

Why must the ampersand (&) always be escaped first?

Because HTML entities begin with an ampersand; if you escape other characters first and then escape ampersands, you double-escape all previous entities.

What are the 5 critical HTML entities to escape?

& (&amp;), < (&lt;), > (&gt;), " (&quot;), and ' (&#39; or &apos;).

What is the difference between named entities and numeric entities?

Named entities use readable names (&copy;); numeric entities use decimal (&#169;) or hex (&#xA9;) Unicode code points.

Related Calculators & Tools