What does the HTML Entity Encoder do?
The HTML Entity Encoder converts reserved markup characters (<, >, &, ", ', `, =) and non-alphanumeric unicode symbols into safe HTML entity codes (<, >, &, ', <). It supports Named Entities, Decimal Numeric Entities, and Hexadecimal Unicode point formats to prevent Cross-Site Scripting (XSS).
Core Concepts
Understanding HTML entity encoding formats:
- Named Entities: Replaces reserved characters with readable W3C mnemonic names (e.g.
<for<,>for>,&for&,"for"). - Decimal Numeric Entities: Formats characters into their decimal ASCII code points (e.g.
<for<,>for>). - Hexadecimal Entities: Encodes characters using their hexadecimal byte representation (e.g.
<for<,>for>). - Full Non-Alphanumeric Mode: Encodes all punctuation, whitespace, and special unicode symbols for strict sanitization.
How to use the tool?
- Enter Raw HTML: Paste your raw HTML markup or user-submitted text into the editor or click Load Sample.
- Select Entity Format: Choose Named Entities, Decimal Numeric Entities, or Hexadecimal Entities, and optionally toggle Encode all non-alphanumeric characters.
- Encode & Copy: Click Encode HTML Entities, then click Copy or Download to export the XSS-safe markup.
Related Developer Utilities
If you work with HTML entities, security sanitization, and markup formatting, explore these complementary tools:
- HTML Entity Decoder: Convert HTML entities back to raw unescaped text.
- HTML Formatter & Beautifier: Format and indent unescaped HTML documents.
- HTML Syntax Validator: Validate HTML syntax and inspect tag nesting errors.
- HTML Tag Stripper: Strip all HTML tags and extract clean plain text.
REST API Integration
Blueutils provides a free REST API endpoint (POST https://blueutils.com/api/html/html-encoder) to programmatically convert reserved HTML markup characters into named, decimal, or hexadecimal HTML entities.
API Request Parameters
| Name | Type | Description | Example |
|---|---|---|---|
rawText |
String | Raw HTML or text payload to encode. | "<div class=\"card\">Hello & World</div>" |
options.mode |
String | Optional. Format mode: "named", "numeric", "hex". Default: "named". |
"named" |
options.encodeAll |
Boolean | Optional. Encode all non-alphanumeric characters. Default: false. |
false |
API Request Payload Examples
cURL
curl -X POST https://blueutils.com/api/html/html-encoder \
-H "Content-Type: application/json" \
-d '{
"rawText": "<div class=\"card\">Hello & World</div>",
"options": { "mode": "named", "encodeAll": false }
}'Python
import requests
url = "https://blueutils.com/api/html/html-encoder"
payload = {
"rawText": "<div class=\"card\">Hello & World</div>",
"options": { "mode": "named", "encodeAll": False }
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.json())Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
String jsonPayload = """
{
"rawText": "<div class=\\"card\\">Hello & World</div>",
"options": { "mode": "named", "encodeAll": false }
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://blueutils.com/api/html/html-encoder"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}API Response Parameters
| Name | Type | Description | Example |
|---|---|---|---|
isValid |
Boolean | Indicates whether encoding succeeded. | true |
mode |
String | Format mode applied (named, numeric, hex). |
"named" |
entityCount |
Number | Total count of special characters converted. | 4 |
encodedText |
String | HTML entity-encoded string. | "<div class="card">Hello & World</div>" |
output |
String | Encoded HTML output string. | "<div class="card">Hello & World</div>" |
API Response Payload Examples
Success Response (HTTP 200 OK)
{
"isValid": true,
"mode": "named",
"encodeAll": false,
"entityCount": 4,
"encodedText": "<div class="card">Hello & World</div>",
"output": "<div class="card">Hello & World</div>"
}Validation Failure Response (HTTP 400 Bad Request)
{
"isValid": false,
"error": "Invalid input: Text payload to HTML encode cannot be empty."
}Rate Limit Exceeded Response (HTTP 429 Too Many Requests)
{
"error": "API rate limit exceeded. Please wait or contact support@blueutils.com."
}Why use an API to encode HTML entities?
Integrating the HTML Entity Encoder API into backend template engines, CMS publish pipelines, or AI agent tool calling provides key benefits:
- Rapid Script Validation: Escapes untrusted user input before storing in databases or rendering dynamic HTML emails to prevent XSS injection.
- Optimized Token Efficiency for AI Agents: LLMs frequently mix named and numeric entities inconsistently. Calling the API encodes characters deterministically without consuming token budget.
- Deterministic Accuracy Without Hallucinations: Ensures 100% compliant W3C character entity substitution.
Native Usage
How to encode HTML entities locally in terminal environments or scripts:
Windows (CMD / PowerShell)
# Encode HTML entities in PowerShell
[System.Net.WebUtility]::HtmlEncode('<div class="card">Hello & World</div>')Linux / Unix (Bash)
# Encode HTML entities using Python in Linux
python3 -c "import html; print(html.escape('<div class=\"card\">Hello & World</div>', quote=True))"Python
Using Python html.escape:
import html
raw_text = '<div class="card">Hello & World</div>'
encoded = html.escape(raw_text, quote=True)
print("Encoded:", encoded)Java
Using Java String.replace:
public class HtmlEncoderExample {
public static void main(String[] args) {
String raw = "<div class=\"card\">Hello & World</div>";
String encoded = raw.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace("\"", """)
.replace("'", "'");
System.out.println(encoded);
}
}