The title is rewritten as: "Using UTF-8 encoding throughout"
P粉321676640
P粉321676640 2023-08-21 21:12:13
0
2
525
<p> I'm setting up a new server and want full UTF-8 support in my web application. I've tried this 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 mismatches that can be ruled out? </p> <p>This is a new Linux server running MySQL 5, PHP 5 and Apache 2. </p>
P粉321676640
P粉321676640

reply all(2)
P粉790187507

I would like to add one more point to chazomaticus' excellent answer:

Also don't forget the META tag (like this, or its 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 section dedicated to internationalization (I18N). They have a lot 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粉653045807

data storage:

  • Specify the utf8mb4 character set on all tables and text columns in the database. In this way, MySQL will physically store and retrieve the value in the native encoding of UTF-8. Note that if a utf8mb4_* collation is specified (without explicitly specifying a character set), MySQL will implicitly use the utf8mb4 encoding.

  • In older versions of MySQL (< 5.5.3), you could only use utf8 which only supported a subset of Unicode characters. I hope I'm 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 perform any transformation on the data when passing it to the application and vice versa.

  • Some drivers provide their own mechanism to configure the connection character set, which both updates its own internal state and informs MySQL of the encoding to use on the connection - this is usually the preferred method. In PHP:

    • If you are using the PDO abstraction layer of 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 stuck in 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 to treat the data encoding on the connection: SET NAMES 'utf8mb4'.

  • The same considerations as above regarding utf8mb4/utf8 also apply here.

Output:

  • UTF-8 should be set in the HTTP header, for example 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.
  • If your application transfers text to other systems, they will also need to know the character encoding. For web applications, the browser must know in which encoding to send the data (either via HTTP response headers or HTML metadata).
  • When using json_encode() to encode output, add JSON_UNESCAPED_UNICODE as the second parameter.

enter:

  • The browser will submit the data in the character set specified by the document, so no special processing is required on input.
  • If you have doubts about the request encoding (in case it may have been tampered with), you can verify that each received string is valid UTF-8 before storing or using any data. PHP's mb_check_encoding() can solve this problem, but you have to stick with it. There is no way around this problem, as a malicious client can submit data in any encoding they want, and I have yet to find a trick 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 hard part. You'll probably make heavy 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 cases you should use the equivalent mbstring functions.

  • In order to know what you're doing (i.e. not screw up), you really need to understand UTF-8 and how it works at the lowest level. There are some great resources at the link at utf8.com to learn everything you need to know.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template