2 min read
How to Encode and Decode HTML Entities
Reserved characters like the ampersand and angle brackets can break a web page or expose it to injection. Learn how to encode them to entities and decode them back.
Why HTML Entities Exist
HTML entities are stand-in codes that let you display characters a browser would otherwise treat as markup. The classic example is the less-than sign: type it directly and the browser starts looking for a tag, so you encode it as an entity to show it as plain text instead.
There are five reserved characters that most often need encoding: the ampersand, the less-than sign, the greater-than sign, the double quote, and the apostrophe. Anytime these appear in content that a browser will render as HTML, encoding them keeps your page displaying what you actually meant.
Named Versus Numeric Entities
Entities come in two forms. Named entities use a readable label, while numeric entities use the character code point. Both render identically, but named entities are easier to recognize at a glance and numeric entities can represent any character even when no name exists.
For everyday reserved characters, named entities are the common choice because they are short and self-documenting. For unusual symbols, emoji, or characters from other writing systems, numeric entities are the reliable fallback since every character has a code point even if it has no friendly name.
Why You Encode the Ampersand First
Order matters when you encode. The ampersand is the character that begins every entity, so if you encode the other reserved characters before it, you will then re-encode the ampersands you just created and corrupt the output. This mistake is known as double-encoding.
The safe rule is to always encode the ampersand first, then handle the remaining reserved characters. Doing it in that sequence means every ampersand in the original text is converted exactly once and none of the freshly created entities get mangled. Decoding simply reverses the process, expanding both named and numeric entities back to the characters they represent.
Encoding and Decoding Step by Step
The HTML Entity Converter handles both directions and takes care of the ordering for you. Here is the workflow for converting text either way.
- 1Open the HTML Entity Converter and paste your text into the input area.
- 2Choose encode to turn reserved characters into entities, or decode to turn entities back into characters.
- 3Review the output and confirm the ampersands were handled cleanly with no double-encoded fragments.
- 4For decoding, check that both named and numeric entities expanded to the characters you expected.
- 5Copy the converted text and drop it into your HTML, template, or email.
When to Encode and When Not To
Encode any user-supplied or dynamic text before you place it inside a page as HTML. This is the single most important habit for preventing broken layouts and cross-site scripting, since an unescaped angle bracket in user input can open a tag you never intended.
You generally do not need to encode text that is already inside a plain text field, a code block that the browser will not interpret, or content your framework escapes automatically. When in doubt, encode - a correctly encoded string always renders as the literal characters you wanted.
Frequently asked questions
Which characters must I encode in HTML?
The five reserved characters are the ampersand, the less-than sign, the greater-than sign, the double quote, and the apostrophe. Encoding these keeps a browser from mistaking your text for markup.
What is double-encoding and how do I avoid it?
Double-encoding happens when an ampersand that already begins an entity gets encoded again, producing garbled output. Avoid it by always encoding the ampersand first, then the other reserved characters, which is what the converter does automatically.
Do named and numeric entities look different on the page?
No. A named entity and its numeric equivalent render as the exact same character. Named entities are just easier for people to read in the source, while numeric entities can cover characters that have no name.
Tools mentioned in this guide
HTML Entity Converter
Encode special characters to HTML entities and decode them back.
Developer Tools
URL Encoder / Decoder
Percent-encode text for URLs or decode encoded URLs back to readable text.
Developer Tools
Base64 Encoder / Decoder
Encode text to Base64 or decode it back — full Unicode support.
Developer Tools
Character Counter
Count characters with and without spaces — perfect for length limits.
Text Tools
JSON Formatter
Format, validate, and minify JSON with precise error locations.
Developer Tools
Keep reading