I’m setting up a new server and want to support UTF-8 fully in my web application. I have tried this in the past on existing servers and always seem to end up having to fall back to ISO-8859-1.
Where exactly do I need to set the encoding/charsets? I’m aware that I need to configure Apache, MySQL, and PHP to do this — is there some standard checklist I can follow, or perhaps troubleshoot where the mismatches occur?
This is for a new Linux server, running MySQL 5, PHP, 5 and Apache 2.
utf8mb4character set on all tables and text columns in your database. This makes MySQL physically store and retrieve values encoded natively in UTF-8. Note that MySQL will implicitly use
utf8mb4encoding if a
utf8mb4_*collation is specified (without any explicit character set).
In older versions of MySQL (< 5.5.3), you’ll unfortunately be forced to use simply
utf8, which only supports a subset of Unicode characters. I wish I were kidding.
In your application code (e.g. PHP), in whatever DB access method you use, you’ll need to set the connection charset to
utf8mb4. This way, MySQL does no conversion from its native UTF-8 when it hands data off to your application and vice versa.
Some drivers provide their own mechanism for configuring the connection character set, which both updates its own internal state and informs MySQL of the encoding to be used on the connection—this is usually the preferred approach. In PHP:
$dbh = new PDO('mysql:charset=utf8mb4');
$mysqli->set_charset('utf8mb4'); // object oriented style mysqli_set_charset($link, 'utf8mb4'); // procedural style
If the driver does not provide its own mechanism for setting the connection character set, you may have to issue a query to tell MySQL how your application expects data on the connection to be encoded:
SET NAMES 'utf8mb4'.
The same consideration regarding
utf8applies as above.
- UTF-8 should be set in the HTTP header, such as
Content-Type: text/html; charset=utf-8. You can achieve that either by setting
default_charsetin php.ini (preferred), or manually using
- If your application transmits text to other systems, they will also need to be informed of the character encoding. With web applications, the browser must be informed of the encoding in which data is sent (through HTTP response headers or HTML metadata).
- When encoding the output using
JSON_UNESCAPED_UNICODEas a second parameter.
- Browsers will submit data in the character set specified for the document, hence nothing particular has to be done on the input.
- In case you have doubts about request encoding (in case it could be tampered with), you may verify every received string as being valid UTF-8 before you try to store it or use it anywhere. PHP’s
mb_check_encoding()does the trick, but you have to use it religiously. There’s really no way around this, as malicious clients can submit data in whatever encoding they want, and I haven’t found a trick to get PHP to do this for you reliably.
Other Code Considerations:
You need to make sure that every time you process a UTF-8 string, you do so safely. This is, unfortunately, the hard part. You’ll probably want to make extensive use of PHP’s
PHP’s built-in string operations are not by default UTF-8 safe. There are some things you can safely do with normal PHP string operations (like concatenation), but for most things you should use the equivalent
To know what you’re doing (read: not mess it up), you really need to know UTF-8 and how it works on the lowest possible level. Check out any of the links from utf8.com for some good resources to learn everything you need to know.
I’d like to add one thing to chazomaticus’ excellent answer:
Don’t forget the META tag either (like this, or the HTML4 or XHTML version of it):
That seems trivial, but IE7 has given me problems with that before.
I was doing everything right; the database, database connection and Content-Type HTTP header were all set to UTF-8, and it worked fine in all other browsers, but Internet Explorer still insisted on using the “Western European” encoding.
It turned out the page was missing the META tag. Adding that solved the problem.
The W3C actually has a rather large section dedicated to I18N. They have a number of articles related to this issue – describing the HTTP, (X)HTML and CSS side of things:
- FAQ: Changing (X)HTML page encoding to UTF-8
- Declaring character encodings in HTML
- Tutorial: Character sets & encodings in XHTML, HTML and CSS
- Setting the HTTP charset parameter
They recommend using both the HTTP header and HTML meta tag (or XML declaration in case of XHTML served as XML).
In addition to setting
default_charset in php.ini, you can send the correct charset using
header() from within your code, before any output:
header('Content-Type: text/html; charset=utf-8');
Working with Unicode in PHP is easy as long as you realize that most of the string functions don’t work with Unicode, and some might mangle strings completely. PHP considers “characters” to be 1 byte long. Sometimes this is okay (for example, explode() only looks for a byte sequence and uses it as a separator — so it doesn’t matter what actual characters you look for). But other times, when the function is actually designed to work on characters, PHP has no idea that your text has multi-byte characters that are found with Unicode.
A good library to check into is phputf8. This rewrites all of the “bad” functions so you can safely work on UTF8 strings. There are extensions like the mb_string extension that try to do this for you, too, but I prefer using the library because it’s more portable (but I write mass-market products, so that’s important for me). But phputf8 can use mb_string behind the scenes, anyway, to increase performance.