Always use UTF-8
P粉964682904
P粉964682904 2023-08-27 16:00:18
0
2
409
<p> 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. </p> <p>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? </p> <p>This applies to new Linux servers running MySQL 5, PHP, 5, and Apache 2. </p>
P粉964682904
P粉964682904

reply all(2)
P粉854119263

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

Also don't forget the META tag (like this, or its 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 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 acting as XML).

P粉763662390

data storage:

  • Specify the utf8mb4 character set on all tables and text columns in your database. This makes MySQL physically store and retrieve values ​​encoded natively in UTF-8. Note that MySQL will implicitly use utf8mb4 encoding if a utf8mb4_* collation is specified (without any explicit character set).

  • In older versions of MySQL (< 5.5.3), you'll unfortunately be forced to use simply utf8, which only supports a subset of Unicode characters. I wish I were kidding .

data access:

Output:

  • UTF-8 should be set in the HTTP header, such as Content-Type: text/html; charset=utf-8. You can achieve that either by setting default_charset in php.ini (preferred), or manually using header() 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 or HTML metadata).
  • When encoding the output using json_encode(), add JSON_UNESCAPED_UNICODE as a 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.
  • In case you have doubts about request encoding (in case it could be tampered with), you may verify every received string as being valid UTF-8 before you try to store it or use it anywhere. PHP's mb_check_encoding() does the trick, but you have to use it religiously. There's really no way around this, as malicious clients can submit data in whatever encoding they want, and I haven't found a trick to get PHP to do this for you reliably.

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 process a UTF-8 string, you do so safely. This is, unfortunately, the hard part. You'll probably want to make extensive use of PHP's mbstring extension.

  • PHP's built-in string operations are not by default UTF-8 safe. There are some things you can safely do with normal PHP string operations (like concatenation) , but for most things you should use the equivalent mbstring function.

  • 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 in utf8.com for 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!