You saved a page with a trademark notice, opened it in a browser, and saw ® instead of ®. Or a CSV export from the CMS turned every registered symbol into ®. The natural reaction is to retype the character, but that rarely helps. This garbling is called mojibake, and it is almost never the symbol's fault; the way its bytes are being decoded is.
This guide shows a repeatable diagnosis. You will read the actual bytes the browser receives, find the layer where the encoding breaks, reverse only the corruption that is really there, and add a regression check so the mark survives the next export or database migration.
Table of Contents
- Reproduce the Bug and Read the Bytes
- A Layer-by-Layer Encoding Checklist
- HTML: the meta charset declaration
- HTTP: the Content-Type header
- Files: the editor's save format
- Database: the connection and column charset
- Single vs. Double Mojibake
- Entities vs. Raw Unicode: Choosing the Safer Form
- Add a Regression Check
- Conclusion
Reproduce the Bug and Read the Bytes
Start with a small file that contains a literal ® plus a few other non-ASCII characters. Save it as UTF-8 and serve it from the same stack that produces the broken page.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Encoding probe</title> </head> <body> <p>Acme® — café — 東京 — 😀</p> </body> </html> Do not trust the browser's View Source panel for this step. It shows characters after decoding, which is exactly the layer you are trying to inspect. Fetch the file as bytes instead:
curl -sI https://example.com/probe.html | grep -i '^content-type' curl -s https://example.com/probe.html -o probe.html python3 - <<'PY' data = open("probe.html", "rb").read() start = data.find(b"Acme") print(data[start:start + 6]) print(data[start:start + 6].hex(" ")) PY A healthy UTF-8 response looks like this:
content-type: text/html; charset=utf-8 b'Acme\xc2\xae' 41 63 6d 65 c2 ae The registered sign is Unicode code point U+00AE. In UTF-8 it is two bytes, C2 AE. If those bytes are present and the browser still renders ®, the bytes are fine and the decoder is wrong. If the bytes are C3 82 C2 AE and the page is served as UTF-8, the browser is rendering correctly; the text was already corrupted before it reached the page.
The order in which a browser chooses a decoder matters here. The HTML Standard's encoding sniffing algorithm checks for a byte-order mark first, then a transport-layer charset, and only later the document's own <meta> declaration. That means an HTTP header can silently override a correct-looking meta tag.
Assuming the page is being decoded as UTF-8, the byte patterns tell you where the damage happened:
| Bytes at the symbol | What the browser shows | What it means |
|---|---|---|
c2 ae | ® | Correct UTF-8 |
c2 ae | ® | The page says Windows-1252 or ISO-8859-1 instead of UTF-8 |
c3 82 c2 ae | ® | The stored text is already ®; one repair pass is needed |
c3 83 e2 80 9a c3 82 c2 ae | ® | The stored text is double-encoded; two repair passes are needed |
ef bf bd | � | A byte was already replaced with U+FFFD; the original is lost |
The last row is the important warning. Once a decoder has substituted the replacement character, re-decoding cannot invent the missing byte. Restore that value from a backup or the original source.
A Layer-by-Layer Encoding Checklist
Treat the encoding as a pipeline. Fix the earliest layer that is actually wrong, then re-test the same probe file. Changing several layers at once makes it hard to know which change helped.
HTML: the meta charset declaration
Put the declaration first in the <head>:
<meta charset="utf-8"> MDN's reference for the meta element notes that the charset value must be utf-8 for HTML5 documents and that the declaration must sit entirely within the first 1024 bytes of the file. Replace legacy forms such as <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">. Remember that a meta tag only labels the bytes; it does not convert them, and it loses to a conflicting HTTP header.
HTTP: the Content-Type header
The response should include:
Content-Type: text/html; charset=utf-8 The W3C's guidance on choosing a character encoding warns that an HTTP header declaration overrides the in-page declaration, so a server still sending ISO-8859-1 will defeat a perfect meta tag. Check the header with curl -sI and fix it at the server, reverse proxy, or application layer. In PHP, for example, send it before any output:
header('Content-Type: text/html; charset=utf-8'); Apache has a common trap. The AddDefaultCharset documentation explains that AddDefaultCharset On adds iso-8859-1, while AddDefaultCharset Off disables the default. If every text file on the host really is UTF-8, set AddDefaultCharset utf-8; otherwise leave it off and set the header per virtual host or per application.
Files: the editor's save format
Declaring an encoding does not change the bytes on disk. The W3C guidance puts it plainly: you also have to save the text in that encoding. A template saved as Windows-1252, or a PHP include saved in the system's ANSI code page, can inject bad bytes into an otherwise correct page.
- Choose "UTF-8" or "UTF-8 without BOM" in the editor's save dialog, not "ANSI" or the local code page.
- Check a local file with
file -bi probe.html; the result should mentioncharset=utf-8. - Convert every include and template that contributes output, not just the main page.
- Avoid a UTF-8 BOM in HTML and PHP files. In PHP it is output before your headers and can trigger "headers already sent" errors; in HTML it adds unnecessary bytes before the doctype. A BOM also outranks the HTTP header, so it should be a deliberate choice rather than a side effect of the editor. A BOM is more useful for CSV files that a spreadsheet application must recognize.
Database: the connection and column charset
For MySQL and MariaDB, use utf8mb4 rather than the older utf8 alias for the table or column charset, and make sure the connection uses it too. MySQL's connection character set documentation explains that SET NAMES 'utf8mb4' sets the client, connection, and results character sets together, and that clients can also request the charset through their connection options.
SET NAMES utf8mb4; SHOW SESSION VARIABLES LIKE 'character\_set\_%'; If the session variables report latin1, the database is converting your UTF-8 bytes on the way in or out. Fix the driver or connection string (for example, add charset=utf8mb4) rather than patching individual pages.
If the stored text is already mojibaked, changing the connection alone will not repair it. Repair the data first, then convert the column charset. A blind ALTER TABLE ... CONVERT TO CHARACTER SET utf8mb4 on corrupted text can bake the corruption into the new encoding.
Single vs. Double Mojibake
The pattern is predictable because Windows-1252 is a single-byte encoding. When a UTF-8 byte such as C2 is read as Windows-1252, it becomes Â; the next byte, AE, becomes ®. That produces ®. If the resulting characters are themselves saved as UTF-8 and read as Windows-1252 again, the pattern grows to ®. The Wikipedia article on mojibake documents this repeated corruption, including the same cascading pattern for the pound sign and curly quotes.
| Form | Characters | UTF-8 bytes | Passes to undo |
|---|---|---|---|
® | 1 | c2 ae | 0 |
® | 2 | c3 82 c2 ae | 1 |
® | 4 | c3 83 e2 80 9a c3 82 c2 ae | 2 |
To reverse one pass, encode the visible garbled text back into the bytes it was decoded from, using Windows-1252, then decode those bytes as UTF-8. Python makes the operation explicit:
def undo_mojibake(text): for _ in range(3): try: fixed = text.encode("cp1252").decode("utf-8") except (UnicodeEncodeError, UnicodeDecodeError): return text if fixed == text: return text text = fixed return text for sample in ("®", "®", "Acme® café"): print(repr(sample), "->", repr(undo_mojibake(sample))) '®' -> '®' '®' -> '®' 'Acme® café' -> 'Acme® café' Use cp1252 rather than latin-1 for web-origin mojibake. The curly quotes and dashes that commonly appear in these strings occupy bytes in the 0x80–0x9F range that latin-1 treats as control characters rather than printable characters.
Keep the repair strict and testable:
- Work on a copy and keep a backup until the result is verified.
- Let
UnicodeDecodeErrorstop the pass instead of usingerrors="replace". Python's Unicode HOWTO explains that replacement inserts U+FFFD, which hides the loss instead of recovering it. - Stop when a pass returns the same string or raises an error. Do not keep looping in the hope that more passes will help.
- Do not add a second conversion on data that is already UTF-8. Running a legacy decode function on healthy text is how single mojibake becomes double.
- Compare the result with a known phrase.
Acme® — caféshould come back with®, an em dash, andéintact. - Do not run a global find-and-replace on all content.
Âis a legitimate letter in some languages, and a string that merely contains it is not necessarily mojibake.
If the decoded result contains �, stop. The byte is gone. Restore the value instead of trying another encoding.
Entities vs. Raw Unicode: Choosing the Safer Form
Once the bytes are correct, decide how the symbol should live in the source. Raw UTF-8 ® is usually the best choice in a UTF-8 pipeline: it is one character, it searches and compares naturally, and it survives database round trips without special handling.
An HTML character reference is the safer output when you cannot guarantee the charset of the surrounding document. MDN's character reference glossary lists ® as the named reference for U+00AE. The parser resolves it to the registered sign, while the source itself stays ASCII. That makes ® useful in email templates, CMS widgets, third-party embeds, and legacy pages where the HTTP header is outside your control.
The numeric forms ® and ® are equivalent. For the full set of ways to produce the mark — named entity, decimal and hexadecimal references, raw UTF-8, CSS content, JavaScript, and URL encoding — plus the accessibility and trademark-usage details, see this guide to every reliable way to write the registered symbol in HTML.
An entity is not a repair tool, though. Replacing every ® with ® hides the encoding bug and leaves the rest of the page broken: café still shows café, the em dash still shows —, and the next database export reintroduces the problem. Store the real ® character and let the template escape it at output time when necessary. If a CMS escapes ampersands, a stored ® can also render literally as ® on the page.
Add a Regression Check
Keep one probe string that exercises several byte lengths:
Acme® — café — 東京 — 😀 It covers a two-byte symbol (®), a three-byte dash, an accented letter, three-byte CJK characters, and a four-byte emoji. A database column or connection that only handles three-byte UTF-8 may pass the first four items and fail only on the emoji, so testing just the registered sign is not enough.
Run these checks after any template, header, or database change:
- HTTP header:
curl -sI https://example.com/ | grep -i '^content-type: text/html; charset=utf-8'should print a matching line. - Raw bytes: fetch the page and assert that the UTF-8 encodings of
Acme®,café, and東京are present in the response body. - Database round trip: query the stored value with
SELECT HEX(brand_name) FROM brands WHERE id = 1;. The hex forAcme®starts41636D65and endsC2AE; a value endingC382C2AEmeans the text is still single-encoded. - Export and re-import: export a CSV, open it, and import it again. Confirm that
®andésurvive. If the spreadsheet application assumes the local code page, export that particular CSV with a UTF-8 BOM; keep the BOM out of the HTML pages. - Template scan: search templates and content for the usual patterns, such as
Ã.,Â., andâ€. Treat matches as suspects, not proof, becauseÂcan be a legitimate character.
The check that matters most is the one you run in the browser after clearing the cache or using a private window. DevTools shows the response headers under the Network panel, which is often enough to catch a server that is still announcing the wrong charset.
Conclusion
The registered symbol is a useful canary because its UTF-8 form is short and its mojibake forms are distinctive. Use that to your advantage:
- Read the raw bytes first.
c2 aeis correct UTF-8;®with those bytes means a declaration problem, whilec3 82 c2 aemeans the stored text is already corrupted. - Respect the precedence order: a BOM beats the HTTP header, and the HTTP header beats the meta tag. Fix the header before rewriting content.
- Undo only the corruption you can prove. Encode with
cp1252, decode as UTF-8, keep the operation strict, and stop when the result matches a known phrase. - Use UTF-8 end to end: the meta tag, the
Content-Typeheader, the editor save format, and autf8mb4database connection. - Prefer raw
®in a healthy UTF-8 pipeline, and use®where ASCII-only source is a real constraint — not as a way to mask a broken decode. - Keep the mixed-script probe string in your regression tests so the symbol survives the next copy, export, and import.
