HTML special character escape/entity reference mutual conversion tool | ZeroTools

Replaces the 5 characters `&`, `<`, `>`, `"`, and `'` with entity references when you want to display HTML code as text. Unescaping only applies to these 5 notations plus `&apos;`. It is not a comprehensive decoder for arbitrary named or numeric character references, nor is it a sanitizer covering all XSS countermeasures.

Loading tool interface...

When to use this tool

Back to the tool ↑

Built for quick bidirectional conversion between text and five standard HTML entities (&, <, >, ", ') with real-time field synchronization.

Try it first

Paste '<p>A & B</p>' into the input box to instantly see it converted into '&lt;p&gt;A &amp; B&lt;/p&gt;'.

Before you start

Unescaping only supports the five fixed entities once. Does not decode arbitrary named/numeric entities or sanitize malicious HTML for execution.

Local Browser Processing and Network Communication Notice

This tool's processing operates locally within your browser. Standard site assets, analytics, and advertisements may communicate separately. There are no guarantees.

ZeroTools: Browser Processing & Privacy

ZeroTools focuses on tools that process input on your device. Check each tool’s scope and limitations before use.

Processing and privacy policy
Chapter 1

The 5 target special characters and their conversion specifications

In HTML, a markup language used to describe web pages, certain characters have special meanings (acting as meta-characters) to define structure, such as starting or ending tags and enclosing attribute values.

If you want to display these characters as plain text on the screen, writing them directly will cause the browser's parser to misinterpret them as part of the HTML code, leading to broken layouts or unintended behavior.

The basic process to prevent this is HTML escaping.

This tool is specifically implemented to convert the following 5 special characters, which are the most important and frequently require escaping in basic HTML syntax.

- & (ampersand) is converted to &amp;.

- < (less than) is converted to &lt;.

- > (greater than) is converted to &gt;.

- " (double quotation) is converted to &quot;.

- ' (single quotation) is converted to &#39;.

These are the 5 representative characters that hold special meaning in HTML. The necessary conversions differ depending on the output context, such as text, attribute values, URLs, or JavaScript.

For example, suppose the string <div class="test">A & B's</div> is submitted from a user input form. If this is output directly into HTML, the browser will interpret it as the start of a new div tag, but by using this tool to perform escaping, it is replaced with the notation &lt;div class=&quot;test&quot;&gt;A &amp; B&#39;s&lt;/div&gt; using character references.

When handled as normal HTML text, it becomes less likely to be interpreted as a tag. Actual security requirements vary depending on the output context.

This tool does not have extra features and only provides the mechanical conversion of these 5 characters.

Chapter 2

Conversion order and caution against double escaping

When multiple special characters are mixed in text, simply repeating string replacements can lead to unintended results. The handling of the ampersand (&) is particularly problematic.

Because all HTML entities take a format starting with an ampersand and ending with a semicolon (e.g., &lt;), making a mistake in the replacement order will cause a phenomenon called "double escaping," where the ampersand inside an already converted entity is converted again.

The escape function running inside this tool strictly controls the execution order of replacement processes so as not to replace references generated in a single conversion process again.

Specifically, it first applies the process of replacing & with &amp; to the input string. After neutralizing all independent ampersands present in the string, it sequentially executes the replacements for the remaining <, >, ", and '.

By following this order, it avoids reconverting the ampersands of already generated references later in the same process. However, if the string already contains &amp; or similar at the time of input, the leading & will also be converted, so this is not an automatic double escape detection.

Similarly, in the unescape function that reverts entities back to their original characters, the processing order has an extremely important meaning.

In the reverse conversion, opposite to the escape process, it is designed to execute the replacement from &amp; to & at the very end.

If it were to execute the &amp; replacement first, for example, if the input text was &amp;lt;, it would become &lt; in the first process, and there is a risk that it would be unintentionally decoded further into < in subsequent processes.

This tool adopts a phased approach of reverting only the remaining &amp; to & at the final stage, after restoring all other entities like &lt; and &quot; to their original symbols, making it a targeted and predictable string conversion.

Chapter 3

Specifications and scope of limited reverse conversion (unescaping)

One of the important features of this tool is that the supported scope of reverse conversion (unescaping) is intentionally limited.

The current HTML5 specification system defines numerous named character references (entities), including mathematical symbols, Greek letters, and various symbol types.

Furthermore, there are arbitrary numeric character references using decimal and hexadecimal numbers.

However, this tool does not perform exhaustive decoding of all these HTML entities. This is because it does not have the role of DOM parsing or a complete HTML parser, and its sole purpose is to quickly switch back and forth between the specific target characters mentioned above.

Therefore, the targets of reverse conversion in this tool are strictly limited to the 5 types of escape output results defined in Chapter 1 (&amp;, &lt;, &gt;, &quot;, &#39;), plus &apos;, which is another entity representation of the single quote.

If any other entities are input, the tool does not consider them targets for conversion and outputs the input string while preserving it as plain text.

For example, even if &nbsp; representing a space, &copy; representing the copyright mark, or other numeric character references are input into the text area, they will not be converted into their original symbols or blank characters.

This specification is intended to prevent unexpected character corruption or unintended data alteration, presenting users with a clear boundary of "what the tool converts and what it does not convert." When using this tool, please correctly understand this limited unescaping specification and utilize it as a basic mutual conversion utility for the 5 characters, rather than for the purpose of parsing arbitrary entire HTML contents.

Chapter 4

Bidirectional text editing and clipboard integration features

To maximize user work efficiency, this tool features an intuitive and seamless user interface. Two areas are arranged in parallel on the screen: an area to input the "original HTML text (decoded)" and an area to input the "escaped text (HTML entities)".

These two text areas are linked bidirectionally in real-time through internal state management.

When you enter raw HTML tags or text in the left area, the escaping process is executed instantly with every keystroke, and the entitized results are displayed in the right area.

Conversely, if you paste or edit already escaped data in the right area, the unescaping process runs instantly, restoring the original text in the left area.

With this bidirectional binding, users can obtain their desired results simply by inputting their data into the appropriate area without hesitation, regardless of which state the data they currently have is in.

Furthermore, the bottom of each input area is equipped with a function to count and display the number of characters currently entered, which is useful for checking data length.

It also features a clipboard integration function to use the conversion results in other applications or editors. Just by clicking the "Copy" button, the contents of the text area are transferred to the OS clipboard.

Note that in this English implementation, the clipboard icon temporarily changes to a checkmark without waiting for a Promise success and lacks failure catching, so the icon alone is not absolute proof of a completed copy.

It also provides a one-click "Sample" load function and a "Clear All" function, and all these series of actions are processed entirely within the browser, realizing nimble operability.

Chapter 5

Fundamental differences from HTML sanitization and limitations

In the context of web development and security, the terms "escaping" and "sanitization" are often confused, but they refer to completely different technical processes.

To use this tool safely, it is essential to correctly recognize the fundamental difference between the two and the functional limitations of this tool.

The "escaping" provided by this tool is nothing more than a process that simply replaces specific characters appearing in a string with different strings (entities).

It has no ability to understand the meaning or context of the input data, and mechanically converts the 5 special characters according to rules.

On the other hand, "sanitization" refers to the process of interpreting the structure of the input HTML, allowing only safe tags and attributes, and removing tags that could execute malicious scripts or event handler attributes (such as onload).

This tool does not implement any logic to parse HTML DOM trees or permission lists for HTML tags. Therefore, applying the escape processing of this tool is not a general sanitization guarantee and does not ensure that cross-site scripting (XSS) attacks can be completely prevented.

For example, if you want to remove only dangerous scripts while retaining the HTML decorations input by the user, a simple escape like this tool will invalidate all tags, failing to meet the requirement.

In such cases, you must use a dedicated HTML sanitizer library rather than this tool. It is required to utilize this tool with the understanding of its limitation that it is merely a simple replacement tool for "outputting the entire string as text."

Chapter 6

Safety checks and proper operation by output context

When performing escape processing in web applications, the most important point to note is that the requirements for ensuring safety change depending on "where (the context) in the HTML that data is output".

The 5-character conversion executed by this tool is sufficiently effective in preventing the browser from interpreting them as tags when the data is output as the content of a normal HTML element (e.g., text inside a <p> tag).

However, caution is required when the output destination is an HTML attribute value (e.g., <input value="...">). If double quotes are used as the enclosing quotes for the attribute, escaping the double quotes within the data can prevent breaking out of the frame, but in special cases where the quotes are omitted, further consideration is needed.

Quotes are also targets for conversion, but you should not judge safety based on this alone; it is premised on the developer correctly understanding the structure of the output destination.

More importantly, the processed results of this tool must not be output directly into JavaScript string literals. In the JavaScript context, escaping HTML entities alone is insufficient, and appropriate escaping for characters that have meaning to the JavaScript parser, such as newline codes and backslashes, is mandatory.

This tool is not equipped with JavaScript escaping or percent-encoding features for embedding in URL parameters. Rather than blindly embedding the strings generated by the tool anywhere, be sure to confirm whether they match the actual output context and strive for proper operation.

Frequently Asked Questions (FAQ)

A.
It converts & to &amp;, < to &lt;, > to &gt;, double quotes to &quot;, and single quotes to &#39;. The & is processed first.
A.
No, it cannot. Only &amp;, &lt;, &gt;, &quot;, &#39;, and &apos; are supported. &nbsp; and general numeric character references will remain as they are.
A.
It does not detect it. If you enter a string that already contains &amp; etc. into the left field, the leading & will also be converted. Please enter raw characters in the left field and converted strings in the right field.
A.
With only the replacement of 5 characters, it does not permit or remove tags and attributes, nor does it inspect URL or JavaScript contexts. Please separately use countermeasures suitable for your output destination.