HTML Entity Encoder

Characters encoded—
Input characters—
Encoded length—

How HTML Entity Encoder is calculated: Five characters are encoded: & becomes &, < becomes <, > becomes >, " becomes ", and ' becomes '. Everything else passes through unchanged, so the encoded text is safe to paste into any HTML page.

What is an HTML entity encoder?

An HTML entity encoder turns the characters that have a special meaning in HTML into safe codes called entities, so they show as text instead of being read as code. Type Tom & Jerry <3 "quotes" and 'apostrophes' — 5 > 3 & 2 < 4 and the encoder gives you Tom & Jerry <3 "quotes" and 'apostrophes' — 5 > 3 & 2 < 4. Nine characters were encoded, the text grew from 57 characters to 92, and every one of those nine is now safe to paste into a web page.

The need comes up whenever text has to appear inside HTML. A less-than sign in ordinary text looks like the start of a tag to a browser, and an ampersand looks like the start of an entity. If you paste them raw, the browser will try to read them as code — and the text will vanish, or worse, run as something it was never meant to be. Encoding them first means the browser shows them exactly as you typed them.

This encoder handles the five characters that matter: the ampersand, the less-than sign, the greater-than sign, the double quote, and the apostrophe. Everything else passes through unchanged — letters, numbers, spaces, and the em dash in the default text are all left exactly as they are, because they need no encoding.

It runs entirely in your browser. Nothing is uploaded, nothing is stored, and there is no account to create. The text you encode never leaves your device, which is how it should be for text that might contain a password, a private message, or a piece of code you are not ready to share.

How HTML entities work: the five characters that matter

The ampersand is the first and most important, because it is the character that starts an entity. To show a literal ampersand in HTML, you write & — an ampersand, the letters 'amp', and a semicolon. The default text has two of them: 'Tom & Jerry' and '3 & 2'. Both become &, and both show as a plain & on the page. If you wrote a raw & followed by letters, the browser would try to read it as an entity name, and a made-up entity name shows as nothing at all.

The less-than and greater-than signs are next. A less-than sign starts a tag, so to show one as text you write <, and to show a greater-than sign you write >. The default text has two less-than signs — '<3' and '2 < 4' — and one greater-than sign, '5 > 3'. After encoding, all three are entities, and the browser shows them as the characters you typed instead of trying to open a tag called '3'.

The double quote and the apostrophe matter inside attribute values. A double quote ends an attribute written with double quotes, so a quote inside the value must be written as ". An apostrophe ends an attribute written with single quotes, so it is written as '. The default text has two of each: two double quotes around 'quotes', and two apostrophes around 'apostrophes'. All four are encoded, and the text is safe in any attribute, in either quote style.

The order matters, and the encoder does it right: the ampersand is encoded first. If the less-than sign were encoded first, it would become < — and then the ampersand in < would itself be encoded into &lt;, which is wrong. Encoding the ampersand first means the entities made by the later steps are never touched again. That one ordering rule is the difference between an encoder that works and one that double-encodes everything it touches.

Named, decimal, and hex entities

Entities come in three forms, and this encoder uses the named form for the five characters it handles. A named entity is an ampersand, a name, and a semicolon: & for the ampersand, < for less-than, > for greater-than, " for the double quote. Names are easier to read than numbers, which is why they are the standard choice for these five.

The other two forms use numbers instead of names. A decimal entity is an ampersand, a hash, a number, and a semicolon: ' is the apostrophe, because 39 is its code in the character table. A hex entity is the same idea in base sixteen: ' is also the apostrophe, because 27 in hex is 39 in decimal. The apostrophe has no short named form that works everywhere — ' exists, but older browsers do not all know it — so this encoder uses the decimal form ', which works everywhere.

Other characters have named entities too — &copy; for the copyright sign,   for a non-breaking space, &mdash; for an em dash — but this encoder leaves them alone. The em dash in the default text stays an em dash, because it needs no encoding: it has no special meaning in HTML, and writing it as &mdash; would only make the text harder to read. The encoder's job is the five characters that break HTML, not every character that has a name.

If you need the reverse — turning entities back into plain text — the HTML encoder-decoder on this site does both directions in one place. This page is the encoder only, and it does the one job with no surprises.

A worked example: a sentence full of special characters

Take the default text exactly as it sits on the page: Tom & Jerry <3 "quotes" and 'apostrophes' — 5 > 3 & 2 < 4. It is 57 characters long. The encoder finds nine characters to encode: two ampersands, two less-than signs, one greater-than sign, two double quotes, and two apostrophes. The output is Tom & Jerry <3 "quotes" and 'apostrophes' — 5 > 3 & 2 < 4, which is 92 characters long — 35 characters longer than the input, because each entity is longer than the character it replaces.

A shorter example shows the same thing with less noise. Type 'a & b & c' — nine characters, two ampersands. The output is 'a & b & c' — 17 characters, two encoded. Each ampersand became five characters, so the text grew by eight. The count row says 2, which is the number of characters encoded, not the number of characters added.

A third case shows what happens with plain text. Type 'Hello world' — eleven characters, no special characters. The output is 'Hello world', unchanged, and the count row says 0. An encoder that changes text with nothing to encode would be a broken encoder; this one leaves clean text alone.

One more check worth doing: encode the output again. The encoded text contains ampersands — every entity starts with one — so encoding it a second time will encode those ampersands too, and & will become &amp;. That is correct behaviour, not a bug: encoding is not meant to be done twice. If your text shows &amp; on a page, it was encoded twice, and the fix is to encode the original once, not to decode the double-encoded version by hand.

Who encodes HTML entities, and why

Web developers encode entities whenever user text goes into a page. A comment box, a username, a search term — any text a visitor types could contain a less-than sign, and if it goes into the page raw, the browser will read it as code. Encoding it first is the simplest defence there is, and it is the first line of defence against cross-site scripting, where an attacker types code into a form and hopes the page runs it.

Bloggers and writers encode entities when they paste code into a post. A code sample with <div> in it must be written as <div>, or the browser will try to make a real div out of it and the sample will vanish from the post. A markdown editor does this escaping automatically when it converts markdown to HTML, which is why pasted code shows correctly in a preview.

Technical writers encode entities in documentation. A help page that explains a URL with an ampersand in it must write &, or the link in the help page will break. The same goes for any page that shows a query string, a piece of HTML, or a command with a quote in it — the characters that mean something to the browser must be encoded, or the page will not show what the writer meant.

Then there are the everyday uses: pasting a snippet into a forum that renders HTML, writing a bio with an ampersand in a company name, or putting a less-than sign in a product description. Anywhere text meets HTML, the five characters are waiting — and an encoder is the fastest way to make them safe.

HTML entity encoder vs HTML encoder-decoder

This page and the HTML encoder-decoder do nearly the same job, and the difference is direction. This page encodes only: text in, entities out. The encoder-decoder does both directions in one place — it encodes text into entities, and it decodes entities back into text. If you only need to encode, this page is the shorter route. If you need to go back and forth, the encoder-decoder is the one.

The encoder-decoder also handles a wider set in some versions, but the five characters are the same on both: ampersand, less-than, greater-than, double quote, apostrophe. The rules do not change between the two pages, because the rules come from HTML itself, not from the tool. An entity that is correct on one page is correct on the other.

One more neighbour worth knowing: the JSON formatter tidies structured data, and JSON has its own escaping rules — a double quote inside a JSON string becomes backslash-quote, not ". Do not use HTML entities inside JSON, and do not use JSON escaping inside HTML. Each format has its own way of making special characters safe, and mixing them up is how text ends up showing backslashes on a web page.

The rule of thumb is simple: encode for the format the text is going into. Text going into HTML gets HTML entities. Text going into a URL gets percent-encoding. Text going into JSON gets JSON escaping. The right encoding in the wrong place is as useless as no encoding at all.

Common entity mistakes

The most common mistake is encoding twice. Encode a text once and it is safe; encode the encoded text again and every & becomes &amp;, which shows on the page as the literal text '&' instead of '&'. If your page shows & where it should show &, the text was encoded twice — go back to the original and encode it once.

Second: forgetting the ampersand. People remember to encode the less-than sign and forget that the ampersand starts an entity, so a raw '&copy' in text vanishes — the browser reads it as the start of an entity called 'copy' and shows nothing. Encode the ampersand first, always, and the rest follows.

Third: using ' for the apostrophe. It looks like the natural choice, and it is valid in modern HTML, but older browsers and some XML parsers do not know it. This encoder uses ', the decimal form, which works everywhere. If a page you did not write uses ', it will usually work — but for text you encode yourself, ' is the safer choice.

Fourth: encoding everything. An encoder that turns every non-English letter into an entity makes text unreadable in the source — a page of é instead of é is correct, but nobody can read it or fix a typo in it. Encode the five characters that break HTML, and leave the rest as they are. And fifth: encoding inside code that is already encoded. A template that encodes its output will double-encode text you encoded by hand — encode at one place, either by hand or by the template, never both.

How to use this HTML entity encoder

Using it takes a few seconds. Type or paste your text into the box — the default is a sentence with all five special characters in it — and the encoded text appears instantly in the box below, with the count of encoded characters above it. Copy the encoded text from the output box with the Copy button, and paste it into your page.

To check a single character, type it alone: '&' becomes '&', '<' becomes '<', '>' becomes '>', a double quote becomes '"', and an apostrophe becomes '''. To check that clean text is untouched, type 'Hello world' and watch the output stay exactly the same, with a count of 0.

Everything runs in your browser, so nothing you type is uploaded or stored. One practical tip: encode the text once, at the moment it goes into the page, and keep the original somewhere safe. If you ever need to edit the text, edit the original and encode it again — editing encoded text by hand is how double-encoding happens.

And if you need to go the other way — entities back to plain text — the decoder is the next tool in this set, and the HTML encoder-decoder does both directions on one page. Encode once, decode once, and never both at the same time.

Frequently Asked Questions

A code that stands for a character with a special meaning in HTML: & for the ampersand, < for less-than, > for greater-than, " for the double quote, and ' for the apostrophe. The browser shows the character the entity stands for, instead of reading it as code — so < shows as a less-than sign, not as the start of a tag.

Because the ampersand starts every entity. If the less-than sign were encoded first, it would become < — and then the ampersand in < would itself be encoded into &lt;, which is wrong. Encoding the ampersand first means the entities made by the later steps are never touched again.

" is the named entity for the double quote, and ' is the decimal entity for the apostrophe (39 is its code in the character table). The apostrophe has a named form, ', but older browsers do not all know it, so this encoder uses ', which works everywhere.

You can, but you should not. Encoding encoded text turns every & into &amp;, which shows on the page as the literal text '&' instead of '&'. If a page shows & where it should show &, the text was encoded twice — go back to the original and encode it once.

More free tools

More calculators and utilities from Sortrun — free, no sign-up.