HTML Entity Encoding: When & and < Are Needed
Last updated: · 3 min read
When you write user-supplied text into an HTML page, characters such as &, < and > can be mistaken for markup and break the layout or open the door to injection problems. Converting those characters into HTML entities makes the browser display them as plain text. The HTML Entity Encoder turns special characters into &, <, >, " and '. If you choose the broader scope, it also converts every non-ASCII character such as é, © or € into a named or numeric entity. The conversion runs in your browser.
Formula
1. Read the text one character at a time. 2. Replace '&' with '&', '<' with '<' and '>' with '>'. 3. Replace the double quote with '"' and the apostrophe with '''. 4. If the scope is 'all', write every non-ASCII character as a named entity (for example € or ©) where one exists, otherwise as a numeric entity (&#NNN;). 5. Keep all other characters unchanged and join the results.
This is not a mathematical formula but a character mapping algorithm. The named entity table follows the WHATWG HTML Living Standard. Because the ampersand is encoded first, the new entities produced in the process are not encoded a second time.
What the inputs mean
- Text
- The text to encode. It might be a user comment, a product name or the value of a form field. Line breaks and spaces are kept as they are.
- Scope
- The 'Only & < > " '' option encodes just the characters that matter in HTML; the 'Also all non-ASCII characters' option additionally converts characters such as é, © and € into entities.
Worked example: Making a priced text safe for HTML
- Input: Price: €5 & "tax" <b> with the scope 'Also all non-ASCII characters'.
- The ampersand becomes &, so the other entities in the output are not encoded twice.
- The double quotes become " and the opening angle bracket of <b> becomes <, so the tag is shown as text instead of being interpreted.
- The euro sign is non-ASCII and becomes the named entity €.
Result: Output: Price: €5 & "tax" <b>. Written into HTML, this displays exactly as the original text in the browser.
How to read the result
Encoding stops the text from being read as markup, but the right rules depend on where the text ends up. Inside an attribute value, quotes must be encoded. If the text goes into JavaScript or a URL, HTML encoding alone is not enough, so use the encoding that matches that context. For security, always use the output in the correct context. This is general information.
Common mistakes
- Thinking that only &, < and > need encoding; inside attribute values the quotes must be encoded too.
- Choosing the broader scope without need; on UTF-8 pages accented characters can be written directly, and converting them makes the source harder to read.
- Encoding text that is already encoded; encoding & again produces &amp;, which displays as the literal text &.
- Assuming HTML encoding is enough inside JavaScript or a URL; each context has its own encoding rules.
- Finding ' for the apostrophe odd and reverting it; it is a standard entity that browsers display correctly.
Frequently asked questions
Which characters must always be encoded?
The characters &, <, >, double quote and apostrophe. They can conflict with the HTML structure, so encoding them is recommended.
Do I need to encode accented characters?
On pages that use UTF-8 and declare it correctly, usually not. The narrower scope is normally enough.
Is the text sent to a server?
No. Encoding runs in your browser and the text is not transmitted anywhere.
Can I reverse the encoding?
Yes, with the HTML Entity Decoder, which converts entities back into characters.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.