"Consistently utilizing UTF-8 encoding"
P粉752479467
2023-08-21 21:15:56
<p>
I'm setting up a new server and want full UTF-8 support in my web application. I've tried this before on existing servers but always had to fall back to ISO-8859-1. </p>
<p>Where do I need to set the encoding/charset? I know I need to configure Apache, MySQL and PHP to achieve this - is there some standard checklist I can follow, or where mismatches can be ruled out? </p>
<p>This is a new Linux server running MySQL 5, PHP 5 and Apache 2. </p>
I would like to add something to chazomaticus' excellent answer:
Also don't forget the META tag (like this, or HTML4 or XHTML version):
This may seem trivial, but IE7 gave me problems before.
I'm doing everything correctly; the database, database connection, and Content-Type HTTP headers are all set to UTF-8, which works fine in all other browsers, but Internet Explorer still insists on using the "Western European" encoding.
It turns out that the page is missing the META tag. After adding it, the problem was solved.
edit:
The W3C actually has a rather large section dedicated to I18N. They have a number of articles related to this issue - describing aspects of HTTP, (X)HTML and CSS:
They recommend using both HTTP headers and HTML meta tags (or XML declarations in the case of XHTML provided as XML).
data storage:
Specify the
utf8mb4
character set on all tables and text columns in the database. This allows MySQL to physically store and retrieve values encoded in UTF-8. Note that if autf8mb4_*
collation is specified (without an explicit character set), MySQL will implicitly use theutf8mb4
encoding.In older versions of MySQL (utf8 which only supports a subset of Unicode characters. I wish I was kidding.
data access:
In application code (such as PHP), no matter what database access method you use, you need to set the connection character set to
utf8mb4
. This way, MySQL doesn't do any conversion to its native UTF-8 when it passes the data to the application or vice versa.Some drivers provide their own mechanism to configure the connection character set, which both updates its own internal state and notifies MySQL to use the encoding on the connection - this is usually the preferred approach. In PHP:
If you are using the PDO abstraction layer for PHP ≥ 5.3.6, you can specify the charset in the
DSN
:If you use mysqli, you can call
set_charset()
:If you can only use pure mysql, but happen to be running PHP ≥ 5.2.3, you can call
mysql_set_charset
.If the driver does not provide its own mechanism for setting the connection character set, you may need to issue a query to tell the MySQL application how you want the data on the connection to be encoded:
SET NAMES 'utf8mb4 '
.Same considerations as above regarding
utf8mb4
/utf8
.Output:
Content-Type: text/html; charset=utf-8
. You can do this by settingdefault_charset
(preferred) in php.ini or manually using theheader()
function.json_encode()
to encode the output, addJSON_UNESCAPED_UNICODE
as the second parameter.enter:
mb_check_encoding()
can do this, but you have to be strict about using it. There is really no way around this problem, as a malicious client can submit data in any encoding they want, and I haven't found a way to reliably get PHP to do this for you.Other code notes:
Obviously, all files you will provide (PHP, HTML, JavaScript, etc.) should be encoded in valid UTF-8.
You need to make sure you do it safely every time you handle UTF-8 strings. This is the very difficult part. You may need to make extensive use of PHP's
mbstring
extension.PHP's built-in string operations are not UTF-8 safe by default. You can safely perform some operations using normal PHP string operations such as concatenation, but for most operations you should use the equivalent
mbstring
functions.To understand what you're doing (ie: don't screw it up), you really need to understand UTF-8 and how it works at the lowest level. Check out any of the links at utf8.com for great resources on everything you need to learn.