How to Escape HTML Characters in User-Provided Text
Learn which five HTML characters to escape, how to turn them into entities, and why escaping alone is not a full security fix.
Every web page is a conversation between text and markup, and the browser tells them apart by looking at a handful of characters. Type 5 < 10 into a comment box and the browser must decide: is that less-than sign part of a sentence, or the start of a tag? Hand it over unescaped and the browser guesses — and when it guesses wrong, your words vanish, the layout breaks, or someone else's code runs in your visitors' browsers. Escaping HTML characters is the small, mechanical habit that stops all of that. This guide explains which characters to escape, shows a worked example with real input and output, and explains the one limit beginners should understand: escaping is context-specific, and it is not a complete security solution on its own.
Why escaping matters
HTML is built from a small set of characters that carry structural meaning. The less-than sign < opens a tag, the greater-than sign > closes one, the ampersand & starts a character reference, and quote characters end attribute values. When these characters appear inside ordinary text, the browser cannot tell by looking whether they are structure or content — so it follows a fixed rule and treats them as structure. That rule is what makes HTML work, and it is also what breaks user-provided text that has not been escaped.
Consider a product review that says I paid < $20 for this & it works great. Pasted raw into a page, the browser reads the less-than sign and tries to parse what follows as markup. The text after it may disappear from the page entirely, or the browser may recover in an unpredictable way and swallow part of the sentence. The reviewer wrote a normal sentence; the page shows a broken one. Nothing malicious happened — the page simply failed to mark the text as text.
The same mechanism, aimed deliberately, is how cross-site scripting (XSS) works. If a page prints a visitor's input back to other visitors without escaping it, an attacker can submit text such as <script> followed by code, and the browser will run that code as part of the page. The attack needs no server vulnerability and no stolen password — just one unescaped output. That is why escaping is not cosmetic: it is the boundary between text a visitor wrote and code the browser executes. Only five characters need escaping in ordinary HTML output, the conversion is fully reversible, and free browser-based tools do it in one click.
The five characters you must escape
Every escaping guide converges on the same five characters. They are the only ones with structural meaning in HTML text and quoted attribute values, and escaping them is sufficient for the most common case — plain text placed between tags or inside a quoted attribute.
| Character | Named entity | Numeric reference | Why it is escaped |
|---|---|---|---|
& | & | & | Starts a character reference; a raw & can be misread as the start of an entity |
< | < | < | Starts a tag; a raw < makes the browser look for markup |
> | > | > | Closes a tag; escaping it keeps the output predictable |
" | " | " | Ends a double-quoted attribute value |
' | ' | ' | Ends a single-quoted attribute value |
Three details in that table deserve attention. First, the ampersand must be escaped before anything else. Escaping works by replacing characters with sequences that start with &, so if you escape < first and then escape ampersands, you escape the ampersand you just created and produce &lt; — double-escaped output that displays literally as < instead of as <. Every correct escaper handles the ampersand first; if you write your own, that ordering is the single most important rule.
Second, everything else can usually stay as it is. Accented letters, symbols such as the euro sign, and emoji do not need escaping when the page is served as UTF-8, which the HTML standard requires for new documents. If your page declares UTF-8, write the real character and save the escaping for the five that matter.
Encode basic characters
The fastest way to escape text is a browser-based converter. FBSubNet's free HTML Escape / Unescape tool runs entirely in your browser — nothing you paste is uploaded — and converts the five special characters into entities with one click. Here is a complete worked example, using a single line of text that contains all five characters at once.
Start with this input, a sentence a user might type into a review or comment form:
Tom & Jerry said "5 < 10" & it's true > 50% of the time
Paste it into the tool, choose Escape, and the output is:
Tom & Jerry said "5 < 10" & it's true > 50% of the time
Read the output character by character and every replacement is one of the five from the table: the ampersand becomes &, the quotes become ", the less-than sign becomes <, the apostrophe becomes ', and the greater-than sign becomes >. Nothing else changed: the words, spaces, digits, and the percent sign are untouched, because they carry no structural meaning in HTML.
Now place that escaped output between two paragraph tags in a page:
<p>Tom & Jerry said "5 < 10" & it's true > 50% of the time</p>
The browser decodes each entity back into its character and displays the original sentence exactly as the user wrote it. The paragraph tags are real markup; everything inside them is text. That is the whole point of escaping: the browser can now tell the two apart with certainty.
To see what happens without escaping, put the raw input in the same place:
<p>Tom & Jerry said "5 < 10" & it's true > 50% of the time</p>
Here the browser meets a raw < followed by a space and 10", which is not a valid tag, so it attempts error recovery — and different browsers recover differently. The escaped version has no such ambiguity, which is why escaping should happen at the moment text is written into the page, every time, without exceptions for text that "looks safe."
If you escape in code instead of a tool, the same rule applies. Most languages ship a standard function: Python has html.escape, PHP has htmlspecialchars, and JavaScript in the browser can escape by assigning text to textContent instead of innerHTML — the browser then treats the value as text by construction, which is the safest option of all. Modern frameworks such as React, Vue, and Django auto-escape template output by default; do not switch that protection off.
Escaping is reversible, too: the same tool decodes in the other direction, turning entities back into plain text — handy when an export from a CMS or feed arrives full of & and < sequences. Unknown named entities are left unchanged rather than guessed at, and unescaping never renders anything: it converts < back into the character < as text, not into a tag.
Validate output context
This is the point most escaping guides skip: escaping alone is context-specific, not a full security solution. The five-character escaping in this guide is correct for two contexts — text between HTML tags, and text inside a quoted attribute value. It is not correct for every place user text can land, and using the wrong encoding for a context leaves the hole open.
Inside an HTML attribute, the quote characters matter most, because a quote ends the value. Given <input value="USER TEXT">, a double quote in the user text would close the attribute early and let the attacker add their own attributes, including event handlers. Escaping the double quote to " closes that path, which is why escaping all five characters, not just the angle brackets, is the safe default.
Inside a JavaScript string, HTML entities do not help at all: a value placed in a script block needs JavaScript string encoding, because the browser parses the script before it applies HTML entity decoding. Text placed in a URL needs percent-encoding, not entities: a space becomes %20 and an ampersand becomes %26, as explained in our guide to URL-encoding spaces and special characters. Each context has its own encoding, and the encodings are not interchangeable.
The security consequence is direct. Escaping user text for HTML output stops an attacker from breaking out of a text node or a quoted attribute, but it does nothing about markup you deliberately allow. If your application lets users submit formatted content — comments with bold text, for example — and you insert that HTML into the page, escaping is the wrong tool entirely: you need a sanitizer, a library that parses the submitted markup and strips everything except a small allowlist of safe tags and attributes. The FBSubNet tool says this plainly on its own page: it is a simple character converter, not a complete HTML sanitizer or a replacement for contextual output encoding.
A simple checklist covers most real-world cases. First, decide the output context before choosing an encoding: HTML text, quoted attribute, JavaScript, URL, or CSS. Second, escape for exactly that context, using the framework's built-in encoder where one exists. Third, never insert user text with innerHTML when a text-only path exists. Fourth, if users may submit markup, pass it through a real sanitizer and never through an escaper. The OWASP XSS Prevention Cheat Sheet lists a separate encoding rule for each context, and it is the reference to consult before building anything that handles untrusted markup.
Common mistakes to avoid
- Escaping the ampersand last. Escape
&first, always. Escaping it after the other characters double-escapes the entities you just created, and the page displays literal<text instead of a less-than sign. - Escaping only the angle brackets. The brackets stop tags, but an unescaped quote still breaks out of an attribute value. Escape all five characters as a set, and let the tool or library do it rather than maintaining a hand-rolled list.
- Using HTML escaping in a URL or a script. Entities belong in HTML. URLs need percent-encoding and JavaScript strings need JavaScript encoding; HTML entities in those contexts are ignored or decoded at the wrong time, and the protection quietly fails.
- Escaping on input instead of on output. If you escape text when it is saved and then escape it again when it is displayed, the page shows double-escaped entities. Store the raw text, and escape once, at the moment it is written into a page.
- Treating escaping as sanitization. Escaping makes text safe to display. It does not make submitted markup safe to insert. If users can submit HTML, use a sanitizer built for that job.
Frequently asked questions
Do I need to escape text that has no special characters? No, but you should escape it anyway. Escaping is an output rule, not a judgement about a particular string. Text with no special characters passes through escaping unchanged, and applying it uniformly means a less-than sign pasted in next month is handled automatically.
Can I just strip the special characters instead? You can, but you change the user's words. Removing a less-than sign from 5 < 10 produces 5 10, which is wrong. Escaping preserves the text exactly; stripping alters it.
Does escaping protect against every XSS attack? No. It protects HTML text and quoted attribute contexts, which covers the most common cases. Attacks through JavaScript, CSS, URL, and deliberately allowed markup need their own encodings or a sanitizer, as explained in the context section above.
Escaping HTML characters is a small habit with an outsized effect: five characters, one ordering rule, and the discipline to apply it at output, in the right context, every time. Run your next snippet through the HTML Escape / Unescape tool, check the output against the table above, and your users' words will display exactly as they wrote them — as text, never as markup.