Summary of common solutions to PHP Chinese garbled characters

Source: Internet
Author: User
: This article mainly introduces the common solutions for PHP Chinese garbled text. For more information about PHP tutorials, see. PHP Chinese garbled characters are one of the common problems in PHP development. PHP Chinese garbled characters sometimes occur on the web page, and some occur in the MySQL interaction process, sometimes related to the operating system. The following is a summary.

I. first, PHP webpage code

1. the php file encoding should match the webpage encoding.

A. If you want to use gb2312 encoding, php needs to output the header: header ("Content-Type: text/html; charset = gb2312") and add a static page To tell the browser web page using what encoding, currently Chinese website development XXX mainly uses GB2312 and UTF-8 two types of encoding.

3. database connection encoding: it refers to the encoding used to transmit data with the database during database operations. Note that it should not be confused with the database encoding, for example, MySQL uses latin1 encoding by default. that is to say, Mysql uses latin1 encoding to store data. data transmitted to Mysql using other encoding will be converted to latin1 encoding.
When we know where encoding is involved in WEB development, we also know the cause of garbled code: The three encoding settings are inconsistent, because most of the encodings are compatible with ASCII, so the English symbols will not appear, and Chinese characters will be unlucky.

5. battle against common errors and solutions:

1. The database uses UTF8 encoding, while the page declarative encoding is GB2312, which is the most common cause of garbled code. In this case, the SELECT data in the PHP script is garbled. you need to use mysql_query ("set names gbk") before querying to SET the MYSQL connection encoding, ensure that the page declarative encoding is consistent with the connection encoding set here (GBK is an extension of GB2312 ). If the page is UTF-8 encoded, you can use: mysql_query ("set names UTF8 ");
Note that it is UTF8 instead of a general UTF-8. If the encoding stated on the page is consistent with the internal encoding of the database, no connection encoding can be set.

Note: In fact, MYSQL data input and output are more complex than described above. MYSQL configuration file my. ini defines two default encodings, they are default-character-set in [client] and default-character-set in [mysqld] to set the encoding used for client connection and database respectively by default. The encoding we specified above is actually the command line parameter character_set_client when the MYSQL client connects to the server to tell the MYSQL server what encoding the client data is received, rather than the default encoding.

2. the page declarative encoding is inconsistent with the file encoding. this rarely happens, because if the encoding is inconsistent, the attacker will see garbled code in the browser when making the page. More often, it is caused by modifying some minor bugs after the release, opening the page with error code, and saving it. Or you can use some FTP software to directly modify files online, such as CuteFTP. the conversion error is caused by incorrect software encoding.

3. some friends who rent a VM clearly confirm that the above three codes are correctly set and there are still garbled characters. For example, the web page is GB2312 encoding, IE and other browsers open but always recognized as a UTF-8, the web page HEAD has been declared is GB2312, manually modify the browser code to GB2312 after the page shows normal. The cause is that the server Apache sets the server's global default encoding, added the AddDefaultCharset UTF-8 in httpd. conf. At this time, the server will first send an HTTP header to the browser, which has a higher priority than the declarative encoding in the page, and the natural browser will recognize the error. There are two solutions. the administrator needs to add adddefacharcharset GB2312 to the virtual machine in the configuration file to overwrite the global configuration, or configure it in the. htaccess directory.

Summary: In a word, the best and fastest solution to PHP Chinese garbled text is that the encoding of the page declaration is consistent with the internal encoding of the database. if the requested page number is inconsistent with the internal encoding of the database, SET the connection encoding, mysql_query ("set names xxx"), and XXX as the connection encoding. it will certainly solve the garbled problem.

The above is a summary of common solutions to PHP Chinese garbled text, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.