TToolbox
&

HTML Entity Encode/Decode

Convert HTML characters and entities

About this tool

HTML Entities are standardized codes defined by the W3C to represent reserved syntax characters and invisible symbols safely inside web pages. Because delimiters like `<`, `>`, `&`, and quotes indicate markup structure, outputting raw user input directly triggers DOM corruption and dangerous Cross-Site Scripting (XSS) vulnerabilities. This tool provides bidirectional conversion between raw text and HTML entities (Named and Numeric decimal/hex), safeguarding web application security.

How to Use This Tool (Step-by-Step)

1

Enter Raw Text or Entity String

Paste unescaped text containing markup symbols, user comment strings, or existing HTML entities into the input box.

2

Select Encode or Decode Mode

Click 'HTML Encode' to convert dangerous symbols into safe entities (e.g., `<` becomes `&lt;`), or click 'HTML Decode' to restore plain characters.

3

Copy Sanitized Entity String

The sanitized result displays immediately. Copy with one click to embed safely inside backend templates or documentation.

Comprehensive XSS Delimiter Escaping

Strictly sanitizes the five core XSS attack vectors (`&`, `<`, `>`, `"`, `'`), completely blocking client-side script injection attempts.

Named & Numeric Entity Support

Supports both human-readable named entities (`&copy;` for ©) and robust numeric decimal (`&#169;`) / hex (`&#xA9;`) entities.

100% Client-Side Air-Gapped

Text transformation runs exclusively inside your browser's local sandbox memory. Internal API payloads remain completely secure.

Web Security Principles & Escaping Gotchas

  • •Escape on output, not on database insert: Storing raw entities in databases corrupts text search and mobile API payloads. Store plain text; escape upon HTML rendering.
  • •Avoid double escaping: Escaping an already encoded string turns `&amp;` into `&amp;amp;`, exposing ugly raw entity codes on the frontend.
  • •HTML escaping does not secure href attributes: Sanitizing HTML entities cannot prevent `javascript:alert(1)` pseudo-protocol links. Always validate URL protocols.

Code & Transformation Examples

Malicious XSS Injection Sanitization Benchmark

Raw Dangerous Markup
<script>alert("XSS 漏洞攻击 & 盗取 Cookie!")</script>
Safely Encoded HTML Entities
&lt;script&gt;alert(&quot;XSS 漏洞攻击 &amp; 盗取 Cookie!&quot;)&lt;/script&gt;

* Once escaped, the browser renders the code as safe visible text rather than executing it as active JavaScript.

JavaScript Native HTML Escaping Implementation

Regex Implementation
function escapeHtml(str) {
  return str.replace(/[&<>"']/g, (m) => ({
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#39;'
  })[m]);
}
Unescape Implementation
function unescapeHtml(html) {
  const el = document.createElement('textarea');
  el.innerHTML = html;
  return el.value;
}

* Leveraging `textarea.innerHTML` is the most lightweight browser-native trick for entity decoding.

Frequently Asked Questions

Q: Should single quotes be escaped as &apos; or &#39;?

A: Always use numeric `&#39;`. While XML supports `&apos;`, legacy versions of Internet Explorer failed to decode it. Numeric entities are 100% universally compatible.

Q: What are common symbol entities like Copyright and Trademark?

A: Copyright © is `&copy;` (`&#169;`); Registered ® is `&reg;` (`&#174;`); Trademark ™ is `&trade;` (`&#8482;`); Non-breaking space is `&nbsp;`.

Q: Do Chinese characters still need numeric entity encoding under UTF-8?

A: No. In the modern UTF-8 era, CJK characters are natively supported without conversion to numeric unicode entities like `&#20013;`. Only reserved markup symbols require escaping.

Related Tools

All processing happens locally in your browser. Your data is never uploaded to a server.
/