HTML Decode: HTML Entities to Text
text
HTML decoding reads HTML character references, not percent triplets. For %20 and its kind use the URL percent-decoder.
Escape text as HTML entities →
How do you decode HTML entities back to text?
HTML decoding reads character references back as the characters they stand for, so escaped markup reads as the text somebody typed.
- Find every ampersand. It is the only character that can open a reference.
- Read to the semicolon. A name goes to the HTML standard's table; &# and digits are a code point in decimal, &#x and digits one in hexadecimal.
- Write the character that reference stands for.
- Leave an ampersand that opens nothing as an ampersand. It is text, not a mistake.
- Read the result. A & B < C becomes A & B < C.
HTML Entities to Text Conversion Table
Five names are predefined and carried by both XML and HTML: & for the ampersand, < for the less-than sign, > for the greater-than sign, " for the double quote and ' for the apostrophe. An HTML character reference starts with an ampersand and ends with a semicolon. A named reference uses a name from the HTML standard's table (&); a numeric one uses &# and decimal digits (A) or &#x and hexadecimal digits (A). The last row is = a no-break space (code point U+00A0), which is not one of the five but is the reference you are most likely to meet.
| Character | Code point | Named | Decimal | Hexadecimal |
|---|---|---|---|---|
| & | U+0026 | & | & | & |
| < | U+003C | < | < | < |
| > | U+003E | > | > | > |
| " | U+0022 | " | " | " |
| ' | U+0027 | ' | ' | ' |
| (no-break space) | U+00A0 | |   |   |
The same value in each encoding
These rows are shared with the other encoders, so a value written here is the value every page in the family writes.
| Value | Written as |
|---|---|
| ampersand | character '&' = percent '%26' = base64 'Jg==' = HTML & |
| less-than sign | character '<' = percent '%3C' = base64 'PA==' = HTML < |
| A | character 'A' = percent 'A' (unreserved, left alone) = base64 'QQ==' = HTML A or A |
Every value below is the one the specification fixes, so a result that disagrees with a row here is a result to check.
| Attribute | Value |
|---|---|
| named reference for nbsp | = a no-break space (code point U+00A0) |
Worked Example: A & B < C to Text
One character at a time, with the working written out. This is the same grid the converter shows when you turn on "show steps".
text
Result: A & B < C = A & B < C
A numeric character reference can write any code point. A named one works only for a name that appears in the HTML standard's table; an ampersand followed by a name that is not in the table stays as literal text.
Read the grid a row at a time: & at U+0026 becomes &; < at U+003C becomes <. Every other character is left exactly as it was, which is what keeps the result readable — an escaped document is still the same text, with four or five characters standing in for one.
A is ASCII 65 = 0x41 = 0o101 = 0b01000001; a is 97 = 0x61 = 0o141 = 0b01100001. Uppercase and lowercase differ by 32, which is bit 5. The number in a numeric reference is that code point, so the decimal and the hexadecimal forms of one character are two spellings of one value rather than two different references. Counting the characters is the quickest check on the result: a reference stands for exactly one of them, however many characters it takes to write, so the text either side of it is untouched and the length of the escaped string tells you nothing about the length of the text it holds.
Check it in reverse
In HTML text only two things must be escaped: a less-than sign, and an ampersand that would otherwise start a character reference. Inside a quoted attribute value the quote character that closes it must be escaped as well. Reading it back is the same rule in reverse: an ampersand opens a reference, a semicolon closes it, and what sits between them names one character. To verify the result by hand, escape the decoded characters again and compare it with what you started from.
Worked examples: HTML entities to text
Each example gives the value first, then the working. Click a value to load it in the converter.
What does & stand for?
& in HTML entities is & in text.
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
What does < stand for?
< in HTML entities is < in text.
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
What does A stand for?
A in HTML entities is A in text.
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
What does stand for?
in HTML entities stands for a no-break space (code point U+00A0).
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
What does > decode to?
> in HTML entities is > in text.
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
What does " decode to?
" in HTML entities is " in text.
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
What does ' decode to?
' in HTML entities is ' in text.
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
What does A decode to?
A in HTML entities is A in text.
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
What does é decode to?
é in HTML entities is é in text.
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
What does & decode to?
& in HTML entities is & in text.
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
What does &amp; decode to?
&amp; in HTML entities is & in text.
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
What does <b> decode to?
<b> in HTML entities is <b> in text.
Method: read from the ampersand to the semicolon and write the character that name or number stands for.
Every example above reverses exactly - write the same characters back as entities.
What Makes an Entity Fail to Decode?
Named reference or numeric reference?
A numeric character reference can write any code point. A named one works only for a name that appears in the HTML standard's table; an ampersand followed by a name that is not in the table stays as literal text.
Does a reference need its semicolon?
For legacy compatibility some named references appear in the HTML standard's table both with and without the closing semicolon, so a decoder may resolve a name that has no semicolon after it. This converter writes the semicolon every time, because a reference without one is only readable by luck.
What case are the hexadecimal digits?
0b marks binary, 0o octal, 0x hexadecimal; a leading # marks a hex colour. Hexadecimal is not case-sensitive (FF = ff); iToolHub writes hex digits in uppercase. A numeric reference is read the same either way.
Is this the encoding you need at all?
The three schemes protect different things and are not interchangeable. these encodings are not interchangeable - telling entities, triplets and Base64 apart sets out which does what.
HTML Entities to Text in Code
Both standard libraries carry this conversion, so the one-liner below is the whole job. Check the result against the table above before you trust it in a pipeline.
Python: html.unescape('A & B') JavaScript: new DOMParser().parseFromString(s,'text/html').documentElement.textContent Common Mistakes
- Escaping a value that is already escaped, which turns an ampersand into &amp; and shows the reference itself on the page.
- Trusting a decoder to fix broken markup. It reads references; it does not repair tags.
- Writing a reference with no semicolon and expecting every reader to guess where it ends.
- expecting this to read %20 - percent-decoding for URLs is what handles a percent triplet.
Frequently Asked Questions
Why does &amp; appear instead of an ampersand?
Because the text was escaped twice. The first pass wrote & for the ampersand and the second escaped that ampersand in turn, so decoding it once leaves & on the page.
Does an HTML decoder decode %20?
No. A percent triplet belongs to a URL, not to markup, and this page reads character references only. A value carrying both needs each scheme undone by its own decoder.
What does A stand for?
The capital letter A. The number in a decimal reference is the code point, so A and A are two spellings of the same character.
Can an entity be written without a semicolon?
For legacy compatibility some named references appear in the HTML standard's table both with and without the closing semicolon, so a decoder may resolve a name that has no semicolon after it.
What is ?
A no-break space: = a no-break space (code point U+00A0). It looks like an ordinary space, but a browser will not break a line at it.
"Can I go back the other way?" Yes. The same value converts straight back in the text to entities direction.
Related Conversions
The rest of the family sits on the full set of text encoding tools.