Home / Developer tools / HTML encode

HTML Encode

Convert special characters into HTML entities, so code and text show on a page exactly as you typed them. Choose what to encode and which entity style to use.

Encoded text

Why encode text for HTML

Some characters have a meaning in HTML. A < starts a tag and an & starts an entity. If you put them in a page as they are, the browser tries to read them as markup. Encoding replaces them with entities, so they are shown as characters. This is how you display code in a page, and it is a basic defense against cross-site scripting (XSS) when you show text from users.

What to encode

OptionEncodesUse it for
Text content& < >Text between tags, such as inside a p or a div
Attribute values& < > " 'Values inside quotes, such as title or alt. The default.
All non-ASCII charactersThe above, and every character above code 127Pages or systems that are not set to UTF-8
Every characterEverything except spaces and line breaksObscuring an email address from simple scrapers

Entity styles

  • Named: &eacute;, &copy;. Easy to read. Characters that have no name fall back to the decimal form.
  • Decimal: &#233;. Works for every character.
  • Hex: &#xE9;. The same as decimal, and it matches the Unicode code point.

The apostrophe

The apostrophe is written as &#39; by default, because it works everywhere. &apos; is valid in HTML5 and XML, but very old browsers did not know it, so choose it only if you need it.

Avoid double encoding

Encoding &amp; a second time gives &amp;amp;, and the page shows the code and not the character. Tick Leave existing entities alone when your text may already contain entities.

Other options

  • Line breaks: keep them, or turn each one into a <br> tag.
  • Emoji and other characters above U+FFFF are encoded as one entity, such as &#128512;, and not as two broken halves.

Know the limits

HTML encoding is right for HTML text and attribute values. It is not enough inside a script, a style block or a URL, which each need their own kind of escaping. The HTML entities guide explains this and shows how to encode in PHP, JavaScript and Python.

To reverse the result, use HTML decode. Look up names in the HTML entities list, or convert entities to other formats with the entity decoder and converter. You can also tidy the result with the HTML formatter.

Frequently asked questions

How do I encode text to HTML entities?

Paste your text, choose what to encode, and copy the result. The default encodes the five characters that matter in HTML.

Which characters must be encoded in HTML?

The ampersand and the less-than sign must always be encoded in text. Greater-than is encoded by habit, and quotes must be encoded inside attribute values.

What is the difference between named and numeric entities?

Named entities such as &eacute; are easier to read. Numeric entities such as &#233; and &#xE9; work for every character.

Does HTML encoding prevent XSS?

It protects text and attribute values on a page. It does not protect script blocks, URLs or style blocks, which need their own escaping.