Skip to content

HTML entity encoder and decoder

Free, no sign-up Runs in your browser

Encode text for HTML element content or for an attribute, and decode named and numeric entities. Explains output encoding for XSS and why context matters.

What to do

For text placed between tags, such as <p>HERE</p>, or inside a quoted attribute. Escapes & < > " and '.

Encoded
The result appears here.

Everything you type here stays in your browser. Nothing is sent to HardenKit or to anyone else.

How to use the HTML entity encoder and decoder

  1. Choose to encode for HTML body, encode for an attribute, or decode.
  2. Paste the text. The result updates as you type.
  3. Copy the result and place it only in the context it was encoded for.

About this tool

HTML gives a few characters a job: < opens a tag, & starts a character reference, and quotes end an attribute value. To show those characters as text you write them as entities, &lt; &amp; &quot;, and the browser draws the character without acting on it. That is HTML encoding, and it is the main defense against cross-site scripting: user text that has been encoded cannot open a tag or leave an attribute.

The right encoding depends on where the text lands. Between tags, or in a quoted attribute, escaping & < > " and ' is enough. In an attribute that may not be quoted, a space ends the value, so the safe rule is to escape every character that is not a letter or a digit. Inside a script block, a style, a URL or an event handler, HTML encoding is the wrong tool and each needs its own. This tool does the first two.

Encode on output, at the moment text is written into a page, and not on input. Filtering input for "bad" characters fails, because what is dangerous depends on a context the input layer cannot know, and it damages honest data such as the name O’Brien. The decoder shows why filters miss things: browsers also accept numeric forms, missing semicolons and names such as &colon;.

Sources

Questions

Which characters must be escaped in HTML?

In element content: & and <, and > for safety. In a quoted attribute value: & and the quote character that surrounds it. Escaping all five, & < > " and ', covers both cases and is what template engines do by default.

Does HTML encoding prevent XSS?

It prevents it where the text lands in element content or a quoted attribute. It does not help inside a script, a style, a javascript: URL or an event handler attribute, because the browser decodes entities there before running the code. Each context needs its own encoding, or better, never put untrusted text there.

Should I encode on input or on output?

On output. Store what the user typed and encode it for the context at the point where it is written into a page. Encoding or stripping on input guesses the context wrongly, corrupts legitimate text and still misses cases.

What is the difference between &#60; &#x3C; and &lt;?

None in effect. They are the decimal, the hexadecimal and the named reference for the same character, the less-than sign. Browsers accept all three, and numeric ones even without the final semicolon, which is why a filter that only looks for &lt; can be bypassed.

Related tools