Definition
- Text is stored as bytes, and a character encoding maps the bytes to characters. Read with an encoding other than the one it was written in, the same bytes turn into other characters: that garbled text is mojibake.
- For example, UTF-8 text read as Windows-1252 turns each Arabic letter into two Latin characters, so «العربية» shows as
العربية. Text in the older Windows-1256 encoding, read the same way, shows asÇáÚÑÈíÉ. - Browsers read a page labelled
iso-8859-1,latin1orasciias Windows-1252: the WHATWG Encoding Standard makes those labels names of the same encoding.
Why it matters
- Visitors see symbols instead of the text.
- Search engines index the symbols as they are, so the page does not come up for the Arabic words you wrote.
- The cause is usually a declaration that does not match the file: a server that sends
charset=windows-1252oriso-8859-1for a page saved in UTF-8, or text stored in a database in the wrong encoding and shown as it is.
Example
Save the files in UTF-8, and declare it both in the server's header and at the top of <head>:
Content-Type: text/html; charset=utf-8html
<head>
<meta charset="utf-8" />
<title>المتجر</title>
</head>The W3C asks that the <meta> fit within the first 1024 bytes of the file, so it is best right after <head>. When the header and the <meta> disagree, the header wins.
Common mistakes
- Declaring UTF-8 without saving the files in UTF-8: the declaration has to match the bytes.
- Fixing the page's declaration while the database still holds text in the wrong encoding: that text needs repair too, by a developer, after a backup.
- Pasting garbled text back into the editor: the symbols become the page's real text, and no declaration can bring the Arabic back.