Online HTML Entity Encoder/Decoder: Safely Handle Special Characters
What Is HTML Entity Encoding
HTML entity encoding converts special characters to safe strings starting with & and ending with ;. It enables safe display of special characters in HTML. Need to encode a string right now? Our HTML entity encoder converts characters in your browser with no upload.
Common HTML Entities
| Character | Entity Name | Entity Number | Description |
|-----------|-------------|---------------|-------------|
| < | < | < | Less-than |
| > | > | > | Greater-than |
| & | & | & | Ampersand |
| " | " | " | Double quote |
| ' | ' | ' | Single quote |
| | |   | Non-breaking space |
| © | © | © | Copyright |
| ® | ® | ® | Registered |
Why You Need HTML Entity Encoding
- Prevent XSS attacks: User input like
<script>becomes safe text - Display special characters: Show
<,>,&in HTML content - Internationalization: Display special symbols and characters
- Template engine output: Ensure dynamic content renders safely
- Email templates: HTML emails need stricter escaping
- Rich text editors: Safely save and display user content
XSS Prevention Example
Dangerous Unescaped Output
<!-- User input: <script>alert('XSS')</script> -->
<div>{{ user_input }}</div>
<!-- Rendered (DANGEROUS!) -->
<div><script>alert('XSS')</script></div>
Safe Escaped Output
<!-- User input: <script>alert('XSS')</script> -->
<div><script>alert('XSS')</script></div>
<!-- Rendered (SAFE) -->
<div><script>alert('XSS')</script></div>
How to Use an Online Tool
Using ToolVault's HTML Entity Encoder/Decoder:
- Enter text to encode or decode
- Choose mode:
- Encode: Convert special characters to HTML entities
- Decode: Convert HTML entities back to original characters
- Supports both entity names and entity numbers
HTML Escaping in Code
JavaScript
// Encode
function escapeHtml(text) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, m => map[m]);
}
// Decode
function unescapeHtml(text) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'"
};
return text.replace(/&|<|>|"|'/g, m => map[m]);
}
Python
import html
# Encode
text = '<script>alert("XSS")</script>'
encoded = html.escape(text)
# Output: <script>alert("XSS")</script>
# Decode
decoded = html.unescape(encoded)
# Output: <script>alert("XSS")</script>
FAQ
What's the Difference Between HTML Encoding and URL Encoding?
HTML encoding handles special characters in HTML documents (< → <). URL encoding handles special characters in URLs (space → %20). They solve different problems.
Why Must & Be Escaped First?
& is the starting character of HTML entities. If & isn't escaped first, subsequent escape sequences get misinterpreted. For example: < without escaping & would be treated as an entity itself.
Does React Auto-Escape?
React automatically escapes variables in JSX. But if you use dangerouslySetInnerHTML, no escaping occurs — you must handle it manually.
This article is brought to you by ToolVault. More developer tools at the homepage.
Related Tools
Related Articles
Character Encoding Troubleshooting Handbook: Mojibake, URL Encoding, and Base64 Explained
Chinese turning into ??? or é”±? %E4%B8%AD unreadable? This handbook classifies by symptom — real mojibake, URL percent-encoding, Unicode escaping, and Base64 with Chinese, plus an encoding primer and a debugging workflow.
Chinese Turns Into %E4%B8%AD (Percent Encoding) in a URL — Is That Normal?
URL Chinese becomes a string of %XX percent-encoding — is that normal? Why encoding is needed, how it works, how to decode, and how to handle it in front-end, with our URL tool.
Base64 Decode Shows Garbled Text or Black Question Marks for Chinese — How to Fix
Base64 decode turns Chinese into mojibake or black question marks? Explains the root cause — Base64 encodes bytes, not characters — and gives command-line, code, and online-tool fixes, all processed locally.