Skip to content
OneKitly

Encoding the Same Text Twice Turns an Ampersand Into &

Published 11/25/2026 · 3 min read · Developer tools

Daniel Okonkwo

Daniel Okonkwo — Front-end developer and tech writer at OneKitly

Web performance · File formats

Checked against 2 sources

View profile →
In short

Escaping is not idempotent, because the escape sequence itself contains a character that needs escaping. Run "Café & <crème>" through the encoder once and you get "Café &amp; &lt;crème&gt;". Run the result through again and the ampersands of those escapes are themselves escaped, giving "Café &amp;amp; &amp;lt;crème&amp;gt;", which a browser renders as the literal text "Café &amp; &lt;crème&gt;". Nothing is corrupted and nothing throws an error — the page simply shows its own plumbing, which is why double encoding is usually found by a reader rather than by a test.

Advertisement

The first pass turns & into &amp;. The second turns that & into &amp; again, and the page displays the escape instead of the character.

The minimum depends on where the text lands

In ordinary text content, escaping the ampersand and the less-than sign is enough; a greater-than sign is harmless there. Inside a double-quoted attribute the double quote must go, inside a single-quoted attribute the apostrophe must, and inside a script or a style block HTML escaping does nothing useful at all because those elements have their own parsing rules. The safe mode escapes all five characters precisely so the same output can be dropped anywhere, at the cost of noisier source.

How double encoding gets into a system

Almost always because two layers each believe they are the one responsible. A form library escapes on the way in, the template engine escapes again on the way out, and the value stored in the database is already escaped so a later export carries the escapes into a spreadsheet. The rule that fixes it is to store text raw and escape once, at the moment of rendering, for the context being rendered into — the decode direction of this tool exists mostly to clean up databases where that rule was broken.

One string, four modes
ModeWhat it escapesResult fragment
Minimal& and < only&lt;crème>
Safe& < > " 'l&#39;unité
Numericeverything above ASCII tooCaf&#233;
Safe, applied twicethe escapes as well&amp;amp;
HTML Entity Encoder / DecoderConvert text to HTML entities and back, escaping <, >, &, quotes and optionally all non-ASCII characters.Try the tool →
Advertisement

Frequently asked questions

Do accented characters need escaping?
Not on a page declared as UTF-8, which is every page produced this century. Writing é as &#233; makes the source larger and harder to search without changing what a reader sees, and it is only worth doing when the file must survive a pipeline that mangles bytes above 127 — an old email gateway, a legacy CMS field, a system whose charset you cannot control. The numeric mode exists for those, not for everyday markup.
Is escaping enough to prevent injection?
Only when it matches the context, which is the part that goes wrong. HTML escaping placed into a URL attribute, a JavaScript string or a CSS value protects nothing, because each of those has a different grammar and different dangerous characters. The safe answer is to let a template engine or framework escape automatically for the context it is inserting into, and to treat manual escaping as something you do to inspect a value here, not as the security boundary itself.

Articles you may find interesting

All guides →
ExplainerOne Emoji Is One Character, Two Units, Four Bytes and TwelveDepending on which layer is counting, 😀 has length 1, 2, 4 or 12. A field limit that does not say which one is a bug waiting for a French accent.ExplainerEscaping a String for JSON: Three Characters Are Mandatory, and One Is a TrapRFC 8259 requires exactly three things to be escaped inside a JSON string. Everything else is optional. The one that actually breaks pipelines is a lone surrogate — legal in JSON text, impossible in UTF-8, and silently replaced the moment your data is written out.ExplainerA Version-1 UUID Names the Machine That Made ItIt looks like 32 random hex digits. Twelve of them are a network card's MAC address and fifteen are the moment of creation, to the hundred nanoseconds.GuideBuilding a URL With Parameters That Survives a Copy-PasteThree encodings, one visible difference: %20 or +. The builder's form mode matches URLSearchParams byte for byte on seventeen values — but give it a base URL with a fragment and every parameter lands inside the hash, where no server sees it.ExplainerWhat Is Inside a JWT — and What It Does Not ProtectA JWT is signed, not encrypted. Anyone holding the token can decode the payload and read every claim in it. Here is a real token, decoded without any key, plus the three attacks the signature is supposed to stop and the one problem it cannot solve.GuideStripping HTML Safely: What a Tag Remover Can and Cannot DoRemoving tags and sanitising HTML are two different jobs. One real fragment run through a naive regex and through a formatting-aware stripper, with script and style contents, block breaks, comments, CDATA and entity order all shown as output.

Related tools

Sources

Spotted a mistake in this article?