What it means to HTML encode text
To HTML encode text is to replace characters that have a special meaning in HTML with entities, so the browser shows them instead of acting on them. A less-than sign becomes <, an ampersand becomes & and a double quote becomes ".
You need it whenever text that is not markup has to sit inside markup. A code sample in a blog post, a user's comment in a template, a title inside an attribute and an example in documentation all break the page if a stray < or & gets through. The reverse is HTML Decoder.
A worked example
A line of HTML and a sentence with quotes:
<a href="/menu?item=crème&size=large">Café & Bar — 5 €</a>
She said "hi" and it's 1 < 2 > 0 😀With the default settings, only the markup characters change:
<a href="/menu?item=crème&size=large">Café & Bar — 5 €</a>
She said "hi" and it's 1 < 2 > 0 😀Choose Also all non-ASCII characters with the named style and the accents, the dash, the euro sign and the emoji change too:
<a href="/menu?item=crème&size=large">Café & Bar — 5 €</a>
She said "hi" and it's 1 < 2 > 0 😀The emoji has no common name, so it falls back to a decimal code. The hex style would give 😀 instead.
The options explained
- What to encode: markup characters only is enough for any page served as UTF-8. Encoding all non-ASCII characters produces pure ASCII output, which survives systems that mangle accents or emoji.
- Style: named writes
éwhen a common name exists and falls back to a number for everything else. Decimal writeséand hex writesé. All three decode to the same character. - Encode quotes: turns
"into"and'into'. Required inside attribute values, optional in plain text between tags. - Leave existing entities alone: keeps an entity that is already written, such as
&or©, instead of encoding its ampersand a second time.
The apostrophe uses ' rather than '. Both work in HTML5, but ' is not valid in HTML 4, so the numeric form is the safe one.
Avoid double encoding with existing entities
Encoding text that is already encoded turns & into &amp;, and the reader sees the literal characters & on the page. That is the usual cause of the stray codes people notice in titles and descriptions.
Tick Leave existing entities alone when the text is a mix of raw characters and entities. A valid entity, either a numeric code or a name from the HTML5 list followed by a semicolon, is kept as it is. A lone ampersand and a made-up name such as &bogus; are still encoded. If the text is already entirely encoded, do not run it again at all.
When to encode and when not to
- Do encode text you want shown literally: code samples, user input, example markup and email template text.
- Do encode values that go inside an HTML attribute, with quotes on.
- Do not encode a whole HTML document you want the browser to render. Every tag would turn into visible text.
- Do not use it for URLs. A web address needs percent-encoding, so use URL Encode.
- Do not use it for text going inside JSON. That needs backslash escapes, which JSON Escape handles.
HTML escape in code
A template engine escapes output for you in most frameworks. When you have to do it by hand, use a library function instead of a regular expression. In Python:
import html
html.escape("Tom & Jerry's <b>", quote=True)In PHP, htmlspecialchars($text, ENT_QUOTES) covers the same five characters, which is the job people search for as htmlspecialchars encode. This page is for one-off jobs and for checking what the right output should look like.
Limits of this tool
- Named style uses a set of common names, such as
é,—and€. Characters without one get a decimal code rather than an obscure name. - Characters outside the basic range, such as emoji, are encoded as one code point, not as two surrogate halves. A broken half of a pair becomes the replacement character
�. - Line breaks and tabs are kept as they are. The tool never adds
<br>tags.
Encoding runs in your browser. What you paste is not uploaded or stored. See the Privacy Policy for details.