How to encode and decode HTML entities
- Encode escapes the five reserved characters
& < > " ', which is what you need to show code or user input safely on a web page. - Encode all non-ASCII also converts accented letters, symbols and emoji, as named entities where they exist or as decimal or hex codes. Use it for old systems, emails or XML that can't handle UTF-8.
- Decode turns any named or numeric entity back into the character, for example when cleaning text exported from a CMS.
- Need one symbol? Search the reference table and click the code to copy it.
Named, decimal and hex entities
Every character can be written in up to three ways. All three display the same in a browser:
| Form | Example for © | Notes |
|---|---|---|
| Named | © | Readable, but only about 2,000 characters have names |
| Decimal | © | Works for any Unicode character (169 is U+00A9) |
| Hexadecimal | © | Same as decimal, matches the U+ code point |
Emoji and characters outside the basic plane use a single entity for the whole character: 😀 is 😀
(😀), not two codes. XML and XHTML only recognise & < > " ',
so use numeric entities there.
When you need entities, and when you don't
- Always escape
<and&in text you insert into HTML, and quotes inside attribute values. Otherwise the browser may read your text as markup, or broken markup. - Showing code examples in a blog post or documentation: encode the snippet so
<div>appears as text instead of creating an element. - Invisible characters such as non-breaking spaces and soft hyphens are clearer as entities, because you can see them in the source.
- You don't need entities for é, €, → or emoji on a modern page saved as UTF-8 with
<meta charset="utf-8">. Typing them directly is simpler and makes the source easier to read.
HTML escaping in code
| Language | Escape | Unescape |
|---|---|---|
| PHP | htmlspecialchars($s, ENT_QUOTES) | html_entity_decode($s) |
| Python | html.escape(s) | html.unescape(s) |
| C# / .NET | WebUtility.HtmlEncode(s) | WebUtility.HtmlDecode(s) |
| JavaScript | Set element.textContent instead of building HTML strings | new DOMParser().parseFromString(s, "text/html").body.textContent |
For URLs use the URL encoder, and for JavaScript or JSON escapes use the Unicode encoder.
Frequently asked questions
HTML entities are codes that represent characters in HTML. They start with & and end with ;, either as a name (©) or as a number (© in decimal or © in hex). Browsers display the character instead of the code.
In text, always escape & as & and < as <. Inside attribute values, also escape the quote you used around the value: " as " or ' as '. Escaping > as > is optional but common. Everything else, including accented letters and emoji, can be typed directly in a UTF-8 page.
(or  ). It looks like a normal space but stops the browser from breaking the line there, which is useful between a number and its unit, such as 10 km. Don't use rows of for layout; use CSS margin or padding instead.
Named entities like © are easier to read. Numeric entities like © work for every Unicode character, including those without a name (for example the rupee sign ₹ is ₹), and are safer in XML, which only knows five named entities. Both display the same in HTML.
Escaping user input before putting it into HTML text is an essential defence against cross-site scripting, but it's context-specific: values inside JavaScript, URLs or CSS need their own escaping. Use your framework's built-in output encoding rather than escaping by hand.
No. Encoding and decoding happen in your browser, and decoding never runs the HTML, so pasted scripts can't execute.
Comments 0
No comments yet.