Full UTF-8
P粉514458863
P粉514458863 2023-10-15 11:44:00
0
2
674

I'm setting up a new server and want full UTF-8 support in my web application. I've tried this in the past on existing servers, but always seemed to end up having to fall back to ISO-8859-1.

Where exactly do I need to set the encoding/charset? I know I need to configure Apache, MySQL and PHP to do this - is there some standard checklist I can follow, or maybe troubleshoot where the mismatch occurs?

This applies to new Linux servers running MySQL 5, PHP, 5 and Apache 2.

P粉514458863
P粉514458863

reply all (2)
P粉135292805

I would like to add one thing tochazomaticus' excellent answer一个>:

Also don't forget the META tag (like this, orits HTML4 or XHTML version):

           

This may seem trivial, but IE7 has given me problems before.

I'm doing everything correct; 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. Adding it solves the problem.

edit:

The W3C actually has a rather largesectiondedicated 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 acting as XML).

    P粉536909186

    data storage:

    • Specify theutf8mb4character set for all tables and text columns in the database. This allows MySQL to physically store and retrieve values natively encoded in UTF-8. Note that if theutf8mb4_*collation is specified (without any explicit character set), MySQL will implicitly use theutf8mb4encoding.

    • In older versions of MySQL (utf8, which only supports a subset of Unicode characters. I wish I was kidding.

    data access:

    • In your application code (such as PHP), regardless of which database access method you use, you need to set the connection character set toutf8mb4. This way, when MySQL passes data to your application, it doesn't convert from its native UTF-8 or 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 use on the connection - this is usually the preferred approach. In PHP:

      • If you are using thePDOabstraction layer for PHP ≥ 5.3.6, you can do this inDSN:

        $dbh = new PDO('mysql:charset=utf8mb4');
      • If you usemysqli, you can callset_charset():

        $mysqli->set_charset('utf8mb4'); // object oriented style mysqli_set_charset($link, 'utf8mb4'); // procedural style
      • If you insist on using plainmysqlbut happen to be running PHP ≥ 5.2.3, you can callmysql_set_charset代码>.

    • If the driver does not provide its own mechanism to set the connection character set, you may have to issue a query to tell MySQL how your application expects the data on the connection to be encoded:SET NAME 'utf8mb4 '.

    • The same considerations apply forutf8mb4/utf8, as described above.

    Output:

    • UTF-8 should be set in the HTTP header, such asContent-Type: text/html; character set = utf-8. You can do this by settingdefault_charset code>in php.ini (preferred), or manually using theheader()function.
    • If your application transfers text to other systems, they also need to know the character encoding. For web applications, the browser must be told the encoding in which to send the data (via HTTP response headers orHTML metadata).
    • When usingjson_encode()to encode the output, addJSON_UNESCAPED_UNICODEas the second parameter.

    enter:

    • The browser will submit data for the character set specified by the document, so no special manipulation of the input is required.
    • If you have doubts about the request encoding (in case it might have been tampered with), you can verify that each received string is valid UTF-8 before trying to store or use it anywhere. PHP'smb_check_encoding()does the following: Trick, but you have to use it religiously. There's really no way around this, as a malicious client can submit data in any encoding they want, and I haven't found a trick to get PHP to reliably do this for you.

    Other code notes:

    • Obviously, all files you will serve (PHP, HTML, JavaScript, etc.) should be encoded using valid UTF-8.

    • You need to make sure that every time you handle a UTF-8 string, it is safe. Unfortunately, this is the hardest part. You may want to make extensive use of PHP'smbstringextension.

    • By default, PHP's built-in string operationsare notsafe for UTF-8.You can safely perform some operations (such as concatenation) using normal PHP string operations, but for most cases you should use the equivalentmbstringfunctions.

    • To know what you're doing (read: not screw it up), you really need to understand UTF-8 and how it works at the lowest level possible. Check out any of the links inutf8.comfor some great resources on everything you need to know. p>

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!