P粉4974634732023-08-21 16:20:46
I would like to add a little bit to chazomaticus' excellent answer:
Also don't forget the META tag (like this, or HTML4 or XHTML version):
<meta charset="utf-8">
This may seem trivial, but IE7 has given 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 Internationalization (I18N) area. 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).
P粉7261339172023-08-21 14:32:48
data storage:
Specify the utf8mb4
character set on all tables and text columns in the database. This way, MySQL will physically store and retrieve the value in its native encoding of UTF-8. Note that if a utf8mb4_*
collation is specified (without an explicit character set), MySQL will implicitly use the utf8mb4
encoding.
In older versions of MySQL (<5.5.3), you can only use 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 does not do any conversion from its native UTF-8 when passing data to the application and vice versa.
Some drivers provide their own mechanism for configuring the connection character set, which simultaneously updates its own internal state and informs MySQL of the encoding to use 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 DSN
:
$dbh = new PDO('mysql:charset=utf8mb4');
If you are using mysqli, you can call set_charset()
:
$mysqli->set_charset('utf8mb4'); // 面向对象的样式 mysqli_set_charset($link, 'utf8mb4'); // 过程化的样式
If you are restricted to using 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 MySQL how your application wants the data encoded on the connection: SET NAMES 'utf8mb4 '
.
The same considerations as above regarding utf8mb4
/utf8
also apply here.
Output:
Content-Type: text/html; charset=utf-8
. You can do this by setting default_charset
(preferred) in php.ini or manually using the header()
function. json_encode()
, add JSON_UNESCAPED_UNICODE
as the second parameter. enter:
mb_check_encoding()
does this, but you must always use it. Since a malicious client can submit data in any encoding they want, there is no 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 that every time you handle UTF-8 strings you do it safely. Unfortunately, this is the hardest 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: not 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 some good resources on everything you need to know.