Building it

’ Instead of Apostrophe: Fix Weird Website Characters

Weird characters on your website, such as ’ where an apostrophe belongs, come from one encoding mismatch. Find the layer that is wrong, then repair the text.

Browser window with three mismatched glyph tiles lifted out of one line of text

The short answer

Your text is stored as UTF-8. Something in the chain is reading it as Windows-1252. That is the entire fault, and it is not a font, a theme or a browser setting on your machine: every visitor sees the same thing you do.

A smart apostrophe is one character, U+2019, stored as the three bytes E2 80 99. A reader that believes those bytes are Windows-1252 finds three separate characters in them: â, € and ™. So one apostrophe becomes ’. The jargon for the result is mojibake.

The useful question is never "what are these characters". It is "which layer is reading UTF-8 as something else". Work the layers in this order and stop at the first one that is wrong:

  1. The encoding the source file or template is saved in.
  2. The charset on the Content-Type header the server actually sends.
  3. The <meta charset> inside the page.
  4. The database connection charset and the column character set.
  5. The stored rows themselves, which may already hold the mojibake.

Layers 1 to 4 stop new corruption. Only layer 5 repairs text that is already damaged, and the two jobs are separate. Fixing a declaration does not clean a corrupted row, and cleaning rows while the declaration is still wrong just corrupts them again on the next save.

Read the garbage before you change anything

The pattern names the cause, so start by reading it. Paste the broken string straight off your page into the Invisible Character Detector: it lists every character with its code point and official name, so ’ comes back as three real characters rather than one mystery. That distinction matters later, because three characters in the database and one character in the database need different fixes.

Here is what each pattern was before something mislabelled it. The byte column is the UTF-8 encoding of the intended character, which is exactly what the mislabelling exposes:

What you meantUTF-8 bytesWhat appears on the page
Apostrophe or right single quoteE2 80 99’
Left single quoteE2 80 98‘
Left double quoteE2 80 9C“
Right double quoteE2 80 9D†and one invisible control
En dashE2 80 93–
Em dashE2 80 94—
EllipsisE2 80 A6…
BulletE2 80 A2•
Non-breaking spaceC2 A0Â and a space
éC3 A9é
üC3 BCü
ñC3 B1ñ
©C2 A9©
°C2 B0°
₹E2 82 B9₹
™E2 84 A2â„¢

Two families cover almost every case:

  • Anything starting †is a three-byte UTF-8 sequence beginning E2 80, which is Unicode's General Punctuation block: curly quotes, dashes, the ellipsis, the bullet. This is the Word and Google Docs family, and the one people paste straight into a search box.
  • A stray à or  is a two-byte sequence: an accented Latin letter, a currency or legal symbol, or a non-breaking space.  directly before a space is the signature of a non-breaking space, and it is the one that shows up in prices and phone numbers.
Diagram tracing one apostrophe from UTF-8 bytes through a mislabelled decoder into three characters
One character leaves as three bytes and arrives as three characters. The bytes never changed: only the label on the decoder did.

It is Windows-1252, not ISO-8859-1

This detail decides whether your repair works, and it is what the guides recommending an ISO-8859-1 conversion get wrong.

In genuine ISO-8859-1, the bytes 80 to 9F are C1 control characters, which draw nothing at all. Decoded as true ISO-8859-1, E2 80 99 is â followed by two invisible controls, so the page would show a bare â. You can see ’, with the euro sign and the trademark sign in it, so the decoder was Windows-1252, where 80 is the euro sign and 99 is the trademark sign.

Browsers guarantee this. The WHATWG Encoding Standard lists iso-8859-1, latin1, cp819, ascii and us-ascii among the labels for windows-1252, which means a page that declares charset=ISO-8859-1 is decoded as Windows-1252 regardless. The label you wrote and the decoder you got are not the same thing.

The consequence is practical. A repair that names ISO-8859-1 maps 80 to U+0080 and 99 to U+0099, so the popular one-line PHP fix, mb_convert_encoding($text, 'UTF-8', 'ISO-8859-1'), does not give you the apostrophe back. It gives you â plus two invisible control characters: the apostrophe is still missing, and the field now carries junk that no screen will show you. The row has to be repaired a second time, in the other direction, before it reads correctly. Both directions were checked while writing this page, using the Windows-1252 table published with the Encoding Standard:

Text
intended : Priya’s café, ₹499 only… “Handmade”
on screen: Priya’s café, ₹499 only… “Handmade”

repair with windows-1252 : Priya’s café, ₹499 only… “Handmade”    round trip exact: true
convert from iso-8859-1  : the apostrophe comes back as â plus U+0080 and U+0099, not recovered

Four shapes, four different faults

Not everything strange is mojibake, and the shape tells you which problem you have:

  • ’ or é: UTF-8 read as Windows-1252. The bytes are intact and the text is fully recoverable.
  • �, the black diamond with a question mark (U+FFFD): a decoder was told to read UTF-8 and found bytes that are not valid UTF-8. Usually the stored text is really Windows-1252 or ISO-8859-1 and the declaration moved to UTF-8 ahead of the data. Recoverable if you can name the real encoding.
  • A plain ?: something already converted the text into an encoding that had no room for that character, and substituted a question mark. This is the answer to question marks instead of apostrophes website, and it is the bad case. Nothing in the data says what the character used to be, so it has to be retyped.
  • A visible &#8217; or &rsquo; in the body text: not an encoding fault at all, but double escaping. Some layer escaped the ampersand of an entity that was already an entity. Paste it into the HTML entity encoder and decoder and decode: if it comes back as a clean apostrophe, the bytes were never damaged and the bug is in your template or editor, not your charset. The same tool run in the encode direction tells you what a character should look like when it is deliberately escaped, which is how you tell an intentional entity from an accident.

Find the layer that is wrong

Five checks, in this order. Each one is a question with a yes or no answer, which is what makes the order worth keeping: the first no is your bug.

Step 1: the file or template

Open the template in your editor and check the encoding indicator, usually in the status bar. It must read UTF-8, and preferably UTF-8 without a byte order mark. If it reads Windows-1252 or ISO-8859-1, the file was saved in that encoding and the bytes on disk are already wrong. Re-save as UTF-8, then confirm the text is still correct, because the editor may have converted the characters rather than relabelling them.

Static HTML, PHP templates, CSV imports and translation files are all worth checking. A single file saved in the wrong encoding is the whole bug on a surprising number of sites.

Step 2: the charset the server actually sends

This is the step the forum threads skip, and it is the one that most often decides what visitors see. Run your page through the HTTP Header Checker and read the Content-Type line. You want:

Text
Content-Type: text/html; charset=utf-8

If the header says charset=iso-8859-1, stop here: that is your bug, and no <meta charset> inside the page can overrule it. If the header names no charset at all, the page's own declaration is used instead, so carry on to step 3. The precedence is not a quirk, it is specified: the HTML Standard's encoding sniffing algorithm takes a byte order mark first, then the encoding from the transport layer with confidence "certain", and only then prescans the document for a meta declaration. A perfect <meta charset="utf-8"> cannot override a wrong header.

Where it is set, in the order the value is usually decided:

  • PHP: default_charset in php.ini, which ships as "UTF-8" and is what PHP appends to the header for you. An application that calls header('Content-Type: text/html; charset=ISO-8859-1') anywhere overrides it, so grep for it.
  • Apache: AddDefaultCharset in the vhost or an .htaccess file. AddDefaultCharset UTF-8 is the value you want; a legacy AddDefaultCharset ISO-8859-1 left in a config file breaks every page it covers.
  • Nginx: the charset directive, set to utf-8.
  • A CDN or proxy in front: check the header at the origin and at the edge, because they can differ.

Step 3: the meta tag, and the first 1024 bytes

Only once the header is right does the page's own declaration matter. It should be the first thing in <head>:

HTML
<meta charset="utf-8">

The HTML Standard limits character encoding declarations to the first 1024 bytes of the document, and browsers prescan only that far. A meta charset pushed below a block of inline CSS, a long comment or a pile of preload hints can fall outside the window and be ignored. Put it immediately after <head>.

Step 4: the database connection and the column

If the page itself is clean and only text from the database is broken, the fault is in the database path, and there are two separate settings:

  • The connection charset. This is the one that corrupts data on the way in and out. In MySQL, the connection must be utf8mb4: SET NAMES utf8mb4, or charset=utf8mb4 in a PDO DSN, or mysqli_set_charset($conn, 'utf8mb4'). A connection left at latin1 while the column is utf8mb4 is how perfectly good pages fill a perfectly good database with mojibake.
  • The column character set. SHOW FULL COLUMNS FROM your_table lists it. utf8mb4 is what you want, not latin1 and not the old three-byte utf8, which cannot hold emoji or several Indic characters.

One MySQL quirk is worth knowing before you touch a stored row. MySQL's latin1 is Windows cp1252, not IANA latin1. The manual is explicit that cp1252, and therefore MySQL, assigns characters to the 0x80 to 0x9f positions that ISO 8859-1 leaves undefined, and that 0x80 is the euro sign. That is exactly why the repair below works through latin1 and why it recovers curly quotes rather than destroying them.

Step 5: the rows that are already wrong

Run one query against a row you know is broken:

Text
SELECT col, HEX(col) FROM your_table WHERE id = 123;

Count the bytes behind one apostrophe. There are three possible answers, and they are three different bugs:

Hex for one apostropheWhat the row holdsWhat fixes it
E2 80 99The correct single character. Something downstream is mislabelling it.Layers 2 to 4. The stored text needs no change at all.
C3 A2 E2 82 AC E2 84 A2The three mojibake characters, each separately encoded as UTF-8. Double encoded through Windows-1252, which is also what MySQL latin1 does.The MySQL repair below.
C3 A2 C2 80 C2 99â and two invisible C1 control characters, double encoded through true ISO-8859-1, which is what iconv and the ISO-8859-1 form of mb_convert_encoding produce.The code repair below, not the MySQL one.

The bottom two rows are both called double encoding, and the difference between them is the Windows-1252 against ISO-8859-1 distinction again, this time written into your rows. Both are reversible, and they need opposite reversals, which is why reading the hex before choosing a repair is worth the one minute it costs.

Five stacked layers from source file to stored rows, each with the check that proves it
Work down until a check fails. The first four stop new corruption; only the fifth repairs text that is already damaged.

To find every place the damage shows up, rather than the one spot you noticed, copy the live page's text and the text you intended, then put both into the text diff tool. It marks every changed run, which turns "the apostrophes look wrong somewhere on the site" into a list you can work through. It is also the check to run after a repair, against a copy taken before it.

Where the curly quotes came from

Nothing here is caused by writing in Word or Google Docs, but that is almost always where the characters enter. Both applications substitute typographic characters as you type: Microsoft documents the behaviour as smart quotes under AutoCorrect, and Google Docs does the same under automatic substitution. Straight quotes become curly, two hyphens become a dash, three dots become an ellipsis.

Paste that into a CMS and you have characters above U+007F in your copy for the first time. They are perfectly valid characters and UTF-8 stores them without complaint. The mojibake appears only because the paste exposes a mislabelled layer that plain ASCII never touched, which is why a site can run for years and then "suddenly" break on one new page.

Two reasonable responses, and one unreasonable one. Fix the pipeline so typographic characters survive, which is the right answer, or decide your copy uses straight quotes and turn the substitution off at the source. The unreasonable response is to keep pasting and then hunt the symptoms every week. If you do want straight quotes, do it deliberately: paste the text into Find and Replace, replace the curly characters with their ASCII equivalents, and paste the result into the CMS.

Repair text that is already stored wrong

Three rules before you run anything.

Back up first. mysqldump --default-character-set=binary your_db > backup.sql takes the bytes as they are, without a conversion on the way out. Every repair below is a bulk overwrite, and a bad one is not reversible without this file.

Test on one row. Run the repair as a SELECT against a single known-bad row and read the output before you let it near an UPDATE.

Run it once. These repairs are not idempotent. Applying the double-encoding fix twice damages correct text.

The wrong label case: the bytes are already right

If the hex shows E2 80 99 but the column is declared latin1, the bytes are correct UTF-8 sitting under the wrong label. Do not convert the characters: change the label. The MySQL manual's documented procedure is to go through a binary type, which drops the character set information without converting anything, and then to the type you want:

Text
ALTER TABLE your_table MODIFY col BLOB;
ALTER TABLE your_table MODIFY col VARCHAR(255) CHARACTER SET utf8mb4;

A direct ALTER TABLE ... MODIFY col VARCHAR(255) CHARACTER SET utf8mb4 on a latin1 column does the opposite of what you want here: it faithfully converts each byte as though it were a latin1 character, which is the conversion that produced your mojibake in the first place. The manual also warns that conversion may be lossy when a character is not in both character sets, which is the next case.

The double-encoded case: the bytes are wrong

If the hex shows C3 A2 E2 82 AC E2 84 A2, the text was converted once too often. The repair reverses that conversion: re-encode the characters as single latin1 bytes, strip the character set label with BINARY, then read those bytes as UTF-8.

Text
-- check first
SELECT col, CONVERT(BINARY CONVERT(col USING latin1) USING utf8mb4) AS repaired
FROM your_table WHERE id = 123;

-- then, once the repaired column reads correctly
UPDATE your_table
SET col = CONVERT(BINARY CONVERT(col USING latin1) USING utf8mb4)
WHERE col REGEXP '(Ã|Â|â)';

The WHERE clause matters more than it looks. A row that was never double encoded does not survive this conversion: after the first step its bytes are not valid UTF-8, and MySQL either warns and hands back NULL or rejects the statement, depending on the SQL mode. Keep the filter tight, and add AND CONVERT(BINARY CONVERT(col USING latin1) USING utf8mb4) IS NOT NULL if you want the statement to skip by itself anything it cannot convert. MySQL's latin1 being cp1252 is what makes this lossless for curly quotes, dashes, the ellipsis and the euro sign. It will fail on any character that cp1252 cannot represent, the rupee sign among them, which is the signal to do the same repair in application code instead:

Text
// PHP: from UTF-8 to Windows-1252 bytes, then read those bytes as UTF-8
$fixed = mb_convert_encoding($broken, 'Windows-1252', 'UTF-8');
Text
fixed = broken.encode("cp1252", errors="strict").decode("utf-8")   # the same two steps

Note the direction in the PHP line: from UTF-8 to Windows-1252. That is the opposite direction from the ISO-8859-1 conversion most guides suggest, and the reason theirs leaves control characters behind.

For the third hex signature, the one with C2 80 in it, swap Windows-1252 for ISO-8859-1 in those two lines, or cp1252 for latin-1 in the Python. Do not use the MySQL statement on those rows: MySQL latin1 has the euro sign at 0x80 and no slot for U+0080, so it writes a question mark and the character is then gone for good.

Flat text, exports and page copy

Not every corrupted string lives in a database. For a CSV export, a column of pasted copy, or a handful of CMS pages, the practical repair is a targeted replacement rather than a conversion: Find and Replace in one pass per pattern, with the patterns taken from the table above. Replace ’ with an apostrophe, “ and †with the right quotes,  with a plain space, é with é, and so on for whatever the detector found.

This is the honest tool for the job when the damage is confined and visible. It is the wrong tool for a whole database, because it fixes only the patterns you thought of, and because an uncommon accented character that nobody noticed stays broken.

The question-mark case

If the page shows plain question marks where apostrophes belong, stop looking for a conversion. A converter met a character its target encoding could not hold and wrote ? instead. The original character is not in the file, the row or the backup taken after that point. The text has to be retyped from the source document, which is the strongest argument there is for taking the binary dump before you experiment.

Stop it coming back

The settings that have to agree, end to end. Mismatch any one of them and the same bug returns on the next page someone pastes into:

  • Templates and source files saved as UTF-8, no byte order mark.
  • Content-Type: text/html; charset=utf-8 on every HTML response, checked at the edge as well as the origin.
  • <meta charset="utf-8"> as the first element in <head>.
  • Database columns utf8mb4, with a utf8mb4 collation.
  • Every database connection set to utf8mb4, including the ones in cron jobs, importers and reporting scripts.
  • accept-charset="utf-8" on forms, and UTF-8 on CSV and JSON imports and exports.
  • Email templates and PDF generators told UTF-8 too: they are the layers people forget, and they corrupt text that the website renders perfectly.

Then re-run the first two checks: the detector on a page of real copy, and the header checker on the page itself. Clean on both means you have fixed the cause rather than one symptom.

Questions people ask

Why does the page look fine on my machine and broken for everyone else?

Because a manual browser encoding override, the fix suggested in most support threads, is stored in your own browser and applies to nobody else. It is a diagnostic, not a repair. If overriding the encoding in your browser makes the page correct, you have proved the bytes are good and the declaration is wrong, which is layer 2 or 3.

Should I declare ISO-8859-1 instead, since the characters came from Windows?

No. Declaring ISO-8859-1 gets you a Windows-1252 decoder anyway, by the Encoding Standard's label list, and it limits you to one legacy repertoire with no rupee sign, no Devanagari and no emoji. UTF-8 everywhere is the only configuration that does not need a second decision later.

Why did mb_convert_encoding($text, 'UTF-8', 'ISO-8859-1') make it worse?

Because it converts in the wrong direction and names the wrong legacy encoding. Applied to raw UTF-8 bytes it produces â plus two invisible C1 control characters; applied to text that is already mojibake it produces a second layer of mojibake. The repair goes the other way: from UTF-8 to Windows-1252, then read the resulting bytes as UTF-8.

Do I need utf8mb4, or is utf8 enough?

utf8mb4. MySQL's old utf8 holds at most three bytes per character, so it cannot store emoji or every character in use in Indian scripts, and an insert carrying one fails or truncates. There is no reason to choose the three-byte version in a new schema.

Will switching to UTF-8 break the pages that currently look right?

Only if the stored bytes are genuinely in a legacy encoding, in which case they need the conversion in step 5 rather than a declaration change. Check the hex of a known row first. Correct UTF-8 bytes under a wrong label never break when the label is corrected, which is the point of checking before converting.

One page is broken and the rest of the site is fine. How?

Almost always one template saved in the wrong encoding, or one row that went in through a connection with a different charset, such as an import script or an older plugin. Work steps 1, 4 and 5 for that page only, and use the diff to confirm nothing else on the site carries the same pattern.

Fix the cause, then the copy

Mojibake is cheap to diagnose and cheap to prevent, and expensive exactly once: when a conversion runs in the wrong direction over live data and the original characters are gone. Read the pattern, find the layer, back up, then repair. In that order.

If the trail runs through an application you cannot change, or the same characters keep returning after every fix, that is usually an encoding mismatch somewhere in the stack rather than in the page, and it is the kind of thing we untangle as part of web application work. For a single page or a single column, the five tools above are enough.

For the characters you cannot see at all, which break logins and lookups rather than headlines, start with how to find invisible Unicode characters, then zero-width spaces explained and how to remove hidden characters from text safely.

Tools mentioned in this post