← All tools/Utilities/Text/HTML Entity Encoder
HTML Entity Encoder
Encode or decode HTML entities so code displays safely on a page.
Encoded text — length
How it works
Character map (encode): & → & < → < > → > " → " ' → ' Decode reverses the map, and also understands numeric forms such as ' and '. Only these five characters change — everything else passes through untouched.
Encoding replaces five characters with their entity codes: & becomes &, < becomes <, > becomes >, " becomes " and ’ becomes '. The order matters: the ampersand is replaced first, because every entity itself contains an ampersand — encoding < before & would turn the & in < into &lt; and corrupt the output. Decoding reverses the map, and also understands numeric forms such as ' and ', which are the same apostrophe written two different ways. Only the five characters in the map ever change; letters, numbers, spaces and all other punctuation pass through untouched, so the encoded text is the same length as the original except for the five replaced characters. The method fits showing code samples on a page and escaping text before it is inserted into HTML. It does not make untrusted input safe on its own — decoding text and then inserting it as HTML is still unsafe.
Worked example: <a href="x">it's</a> is 20 characters. Encoded it is <a href="x">it's</a> — 46 characters, because each angle bracket, quote and apostrophe becomes a longer entity. Decoding reverses it exactly.
Second worked example: the plain text <p class="note">Tom & Jerry</p> is 31 characters. Encoded it becomes <p class="note">Tom & Jerry</p>, which is 57 characters. The growth is easy to account for: 2 angle brackets become < and > (+3 characters each), 2 double quotes become " (+5 each) and 1 ampersand becomes & (+4), so 31 + 6 + 6 + 10 + 4 = 57. Decoding the 57-character version returns the original 31 characters exactly.
Common mistakes when encoding HTML
Three mistakes produce most encoding errors. First, double-encoding: running already-encoded text through the encoder again turns < into &lt;, and the page then shows the literal text “<” instead of “<”. Encode once, from the original text, and never encode the output again. Second, encoding the whole page instead of the snippet: only the code sample needs encoding — encode the surrounding page and the browser shows entities everywhere. Third, forgetting the quotes in attributes. A snippet such as <a href="x"> needs the quotes encoded too, or the browser may read the attribute boundary wrongly when the code is shown inside another attribute. And one safety note: encoding is the safe direction, but decoding is not a cleaning step — decoding text from an unknown source and inserting it into a page as HTML can run scripts, so decode only text you trust, and only to display it as text.
Frequently asked questions
What is an HTML entity?
A short code that stands for a character, such as < for the less-than sign and & for the ampersand. Browsers show the character instead of treating it as markup, which is how code samples display safely on a page. The five this page handles are &, <, >, " and ' — the characters that have a special meaning in HTML.
Why encode HTML at all?
So code shows as text instead of running as markup. Paste a div tag into a page without encoding and the browser treats it as a real element; encode it and visitors see the code itself. The same idea applies to user input: encoding the five special characters before showing text on a page stops a stray <script> tag from being read as code.
Does encoding change anything else?
No — only the five characters in the map change. Letters, numbers, spaces and punctuation pass through untouched, so the encoded text reads exactly like the original apart from the five replacements. That is also why decoding is exact: every entity maps back to one character, and nothing else was ever altered.
What is the difference between encoding and decoding?
Encoding turns characters into entities; decoding turns entities back into characters. Decoding the output of this page returns your original text exactly. Decoding also understands numeric forms such as ' and ', so entities written by other tools decode here too, not just the ones this page produces.
Is my text sent anywhere?
No. Encoding and decoding happen in your browser; nothing is uploaded or stored. That matters for code that is not public yet — a snippet from a client project or an internal tool stays on your own computer while you encode it for a document or a page.
Tell us what you think
Loving this tool, or stuck on something? Tell us — it takes a minute.
We’ll only use your name and email to reply if needed. The tool and page are filled in for you.