HTML Entity Encoder Decoder for Ampersands

A practical guide to encoding and decoding HTML character entities, with special attention to ampersands in source, text and web publishing.

What this helps you understand and accomplish

This guide explains what an HTML entity encoder or decoder does with character references, why the ampersand is the character that needs attention first, and how to convert text safely without confusing entity handling with URL encoding, character encoding or sanitization.

Entity encoding and decoding in context

In HTML source, some characters have structural meaning. A less-than sign may open a tag, a greater-than sign may close it, and an ampersand may begin a character reference or entity. To display such a character as ordinary text, it is written as a named or numeric character reference.

An ampersand is represented as the named entity & or the numeric reference &. A decoder converts those forms back to the ampersand character. An encoder performs the reverse operation.

Working with ampersands in practice

For ordinary visible text, encode an ampersand before it can be mistaken for the start of an entity. The phrase "Tom & Jerry" becomes "Tom & Jerry" in HTML source. The browser then renders the word and rather than interpreting the following characters as an entity name.

This matters most in author-generated content, template values and documentation examples. An unescaped sequence such as © can become a copyright sign when rendered, even if the writer intended to show the literal characters.

Choosing and using an approach

A suitable encoder or decoder should let you inspect the exact character replacements it makes. Look for predictable handling of named and numeric references, especially the output form used for the ampersand and characters that commonly appear next to it.

For decoding, check whether the tool supports common named entities and numeric references in both decimal and hexadecimal notation. A decoder may convert a reference to text, but decoding alone does not make untrusted HTML safe to insert into a page.

What entity conversion does not do

HTML entity encoding is separate from URL encoding. Converting a space to   or an ampersand to & changes how source text is represented in HTML; it does not produce a percent-encoded URL component.

Entity conversion is also separate from character encoding. Character encoding defines how text bytes are interpreted, while entity conversion works at the level of HTML source and displays characters through references.

Do not treat decoding as sanitization. Removing character references can expose markup or executable content, so decoding an untrusted value must be followed by the appropriate sanitization or output-escaping step for the destination context.

Practical examples

Encoding examples

Input|Encoded form
Tom & Jerry|Tom & Jerry
A & B & C|A & B & C
Use <tag>|Use <tag>

Decoding examples

Reference|Decoded character
&|&
&|&
&|&
<|<

Suitability judgment

For tasks involving ampersands in HTML source, an entity encoder or decoder is appropriate when the conversion behavior is explicit and predictable. It is not a substitute for URL encoding or sanitization. If a tool only performs string replacement without documenting the complete set of supported references, verify the output for the specific characters you need before relying on it.

A documented tool that distinguishes local processing from any transmission or retention is preferable when sensitive content is involved. Where documentation does not describe data handling, treat that as a gap rather than assuming the tool processes content locally.

Common conversion table

Character|Named entity|Numeric reference
&|&|&
<|<|<
|>|>
"|"|"