Why escape HTML
Browsers read < as the start of a tag and & as the start of an entity. Showing user text without escaping those characters breaks layouts and opens the door to cross-site scripting (XSS). Escaping the five special characters makes any text safe to place between tags or inside a quoted attribute.
Handy entities
| Entity | Character |
|---|---|
| & | & |
| < > | < > |
| " | " |
| | non-breaking space |
| © | © |
| — | — |
Notes
Decoding understands numeric entities (decimal and hex) and the most common named ones. Unknown names are left as they are.
Frequently asked questions
Do I need to encode accented letters?
Not on a UTF-8 page - tick the option only for old systems that need pure ASCII.
Is escaping enough to stop XSS?
It covers text between tags and quoted attributes; URLs, scripts and styles need their own rules.
What is the difference between &#39; and &apos;?
Both are an apostrophe; ' works in older HTML too.