"Unblocked: The path to UTF-8"
Karen Carpenter
Karen Carpenter 2023-08-21 21:18:02
0
2
483

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

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 a troubleshooting method that can eliminate mismatches?

This is a new Linux server running MySQL 5, PHP 5 and Apache 2.

Karen Carpenter
Karen Carpenter

reply all (2)
P粉184747536

I would like to add something tochazomaticus' excellent answer:

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

This may seem trivial, but IE7 has given me trouble 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. The problem was solved after adding it.

edit:

In fact, the W3C 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 provided as XML).

    P粉032977207

    data storage:

    • Specify theutf8mb4character 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 ifutf8mb4_*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 application code (such as PHP), regardless of the database access method used, you need to set the connection character set toutf8mb4. 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 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 specify thecharsetinDSN:

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

        $mysqli->set_charset('utf8mb4'); // 面向对象风格 mysqli_set_charset($link, 'utf8mb4'); // 过程化风格
      • If you are stuck in puremysqlbut happen to be running PHP ≥ 5.2.3, you can callmysql_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 what encoding to expect the data on the connection:SET NAMES 'utf8mb4'.

    • The same considerations as above regardingutf8mb4/utf8apply here.

    Output:

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

    enter:

    • The browser will submit data according to the character set specified by the document, so no special processing is required on the input.
    • 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()can solve this problem, but you must use it strictly. 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 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 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'smbstringextension.

    • 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 equivalentmbstringfunctions.

    • To know what you're doing (i.e. 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 atutf8.comto learn everything you need to know.

      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!