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.
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 DevToolkit Pro'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 DevToolkit Pro. More developer tools at the homepage.
相关文章
URL Encoding Explained: How to Handle Spaces, Chinese Characters & Special Symbols
Learn how percent encoding works for spaces, Chinese characters, and special symbols. Understand %20 vs +, UTF-8 multi-byte encoding, and avoid double encoding bugs.
Online URL Encoder/Decoder: Percent-Encoding Explained
Learn URL encoding (percent-encoding) fundamentals. Understand when and why to encode URLs, special character handling, and best practices for web development.
Online Unicode Encoder/Decoder: Handle Unicode Character Conversion
Learn Unicode encoding and decoding principles. Understand differences between UTF-8, UTF-16, and Unicode escape sequences, and how to use an online tool for Unicode conversion.