PHP Chinese garbled problem summary

Source: Internet
Author: User
Tags mysql code readfile

PHP Chinese garbled charactersIt is one of the common problems in PHP development.

PHP Chinese garbled charactersYesOccurs on the web page. Some are generated during MySQL interaction and sometimes related to the operating system. The following is a summary.

I. First, PHP webpage code

The best and fastest solution 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"); XXX is the connection code. it will certainly solve the garbled problem.

1. the PHP file encoding should match the webpage encoding. if you want to use gb2312 encoding, PHP needs to output the header: Header ("Content-Type: text/html; charset = gb2312 "), add <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> to the static page. All files are encoded in ANSI format and can be opened in notepad, to overwrite the source file. B. to use UTF-8 encoding, PHP must output the header: Header ("Content-Type: text/html; charset = UTF-8 "), add <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"> to the static page. The encoding format of all files is UTF-8. It may be a little troublesome to save it as UTF-8. Generally, Bom is generated at the beginning of the UTF-8 file. If session is used, problems may occur. You can use editplus to save it in editplus, tool-> Parameter Selection-> file-> UTF-8 signature, select the total is to delete, and then save to remove the BOM information.

2. php itself is not Unicode. All functions such as substr must be changed to mb_substr (mbstring extension is required), or iconv transcoding is used.

Ii. Data Interaction between PHP and MySQL

PHP and database encoding should be consistent

1. modify the MySQL configuration file my. ini or my. CNF, mySQL is better to use utf8 encoding [MySQL] default-character-set = utf8 [mysqld] default-character-set = utf8 default-storage-engine = MyISAM added under [mysqld: default-collation = utf8_bin init_connect = 'set names utf8'

2. in PHP that requires database operationsProgramAdd mysql_query ("set names 'encoding '");. The encoding is the same as that of PHP. If php encoding is gb2312, MySQL encoding is gb2312. If it is UTF-8, MySQL encoding is utf8, in this way, no garbled characters will appear during data insertion or retrieval.

III. PHP is different from the operating system-related Windows and Linux encoding. In Windows, if the PHP function is called with UTF-8 encoding, an error occurs, such as move_uploaded_file (), filesize (), readfile (), and so on. These functions are often used to process uploads and downloads. the following error may occur during the call: Warning: move_uploaded_file () [function. move-uploaded-file]: failed to open stream: invalid argument in... warning: move_uploaded_file () [function. move-uploaded-file]: Unable to move ''' in... warning: filesize () [function. filesize]: stat Fai LED... in... warning: readfile () [function. readfile]: failed to open stream: invalid argument in .. although gb2312 encoding in Linux does not produce these errors, the stored file name becomes unreadable due to garbled characters. In this case, you can first convert the parameter to the encoding recognized by the operating system, encoding conversion can be performed using mb_convert_encoding (string, new encoding, original encoding) or iconv (original encoding, new encoding, string). In this way, the stored file name will not contain garbled characters, you can also normally read files to upload and download files with Chinese names. In fact, there are still better solutions to completely break away from the system, so you don't have to consider the encoding of the system. You can generate a sequence with only letters and numbers as the file name, and store the original Chinese name in the database. In this way, calling move_uploaded_file () will not cause problems, during the download, you only need to change the file name to the original name with Chinese characters. Download Code The following header ("Pragma: Public"); header ("expires: 0"); header ("cache-component: Must-revalidate, post-check = 0, pre-check = 0 "); header (" Content-Type: $ file_type "); header (" Content-Length: $ file_size "); header (" content-Disposition: attachment; filename = \ "$ file_name \" "); header (" content-transfer-encoding: Binary "); readfile ($ file_path); $ file_type is the file type, $ file_name is the original name, and $ file_path is the address of the file stored on the service.

 

4. to sum up the cause of Garbled text, there are two possible causes for Garbled text. The first reason is the incorrect encoding (charset) settings, which causes the browser to parse the Garbled text with wrong encoding, as a result, "tianshu" is messy on full screen. Secondly, the file is opened with an incorrect code and saved. For example, a text file was originally gb2312 encoded, but opened and saved in UTF-8 encoding. To solve the above garbled code problem, you must first know which stages of development involve encoding:

 

1. file encoding: indicates the encoding of the page file (.html,. php, etc. Notepad and Dreamweaver automatically recognize the file encoding when opening the page, so there is no problem. Zendstudio does not automatically recognize the encoding, but it will only open the file with a certain encoding according to the preference configuration. If you do not pay attention during work, use the error code to open the file, after the modification, the garbled code will appear as soon as it is saved (I have a deep understanding ).

 

2. Page declarative encoding: in the HTML code head, you can use <meta http-equiv = "Content-Type" content = "text/html; charset = "XXX"/> 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, therefore, if English Symbols do not appear, Chinese characters are 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 rather than the 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 an adddefacharcharset gb2312 to the virtual machine in the configuration file to overwrite the global configuration, or configure it in the. htaccess directory. Conclusion: In a word, we need to solve the problem.MostPHP Chinese garbled charactersThe fastest solution 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"); XXX is the connection code. it will certainly solve the garbled problem.

Reprinted: http://www.cnblogs.com/showker/archive/2010/08/30/1812673.html

PHP Chinese garbled charactersIt is one of the common problems in PHP development.

PHP Chinese garbled charactersYesOccurs on the web page. Some are generated during MySQL interaction and sometimes related to the operating system. The following is a summary.

I. First, PHP webpage code

The best and fastest solution 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"); XXX is the connection code. it will certainly solve the garbled problem.

1. the PHP file encoding should match the webpage encoding. if you want to use gb2312 encoding, PHP needs to output the header: Header ("Content-Type: text/html; charset = gb2312 "), add <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> to the static page. All files are encoded in ANSI format and can be opened in notepad, to overwrite the source file. B. to use UTF-8 encoding, PHP must output the header: Header ("Content-Type: text/html; charset = UTF-8 "), add <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"> to the static page. The encoding format of all files is UTF-8. It may be a little troublesome to save it as UTF-8. Generally, Bom is generated at the beginning of the UTF-8 file. If session is used, problems may occur. You can use editplus to save it in editplus, tool-> Parameter Selection-> file-> UTF-8 signature, select the total is to delete, and then save to remove the BOM information.

2. php itself is not Unicode. All functions such as substr must be changed to mb_substr (mbstring extension is required), or iconv transcoding is used.

Ii. Data Interaction between PHP and MySQL

PHP and database encoding should be consistent

1. modify the MySQL configuration file my. ini or my. CNF, mySQL is better to use utf8 encoding [MySQL] default-character-set = utf8 [mysqld] default-character-set = utf8 default-storage-engine = MyISAM added under [mysqld: default-collation = utf8_bin init_connect = 'set names utf8'

2. add mysql_query ("set names 'code'") before the PHP program that requires database operations. The encoding is consistent with the PHP code. If the PHP code is gb2312, the MySQL code is gb2312, if it is UTF-8, MySQL encoding is utf8, so no garbled characters will appear during data insertion or retrieval.

III. PHP is different from the operating system-related Windows and Linux encoding. In Windows, if the PHP function is called with UTF-8 encoding, an error occurs, such as move_uploaded_file (), filesize (), readfile (), and so on. These functions are often used to process uploads and downloads. the following error may occur during the call: Warning: move_uploaded_file () [function. move-uploaded-file]: failed to open stream: invalid argument in... warning: move_uploaded_file () [function. move-uploaded-file]: Unable to move ''' in... warning: filesize () [function. filesize]: stat Fai LED... in... warning: readfile () [function. readfile]: failed to open stream: invalid argument in .. although gb2312 encoding in Linux does not produce these errors, the stored file name becomes unreadable due to garbled characters. In this case, you can first convert the parameter to the encoding recognized by the operating system, encoding conversion can be performed using mb_convert_encoding (string, new encoding, original encoding) or iconv (original encoding, new encoding, string). In this way, the stored file name will not contain garbled characters, you can also normally read files to upload and download files with Chinese names. In fact, there are still better solutions to completely break away from the system, so you don't have to consider the encoding of the system. You can generate a sequence with only letters and numbers as the file name, and store the original Chinese name in the database. In this way, calling move_uploaded_file () will not cause problems, during the download, you only need to change the file name to the original name with Chinese characters. The download code is as follows: Header ("Pragma: Public"); header ("expires: 0"); header ("cache-component: Must-revalidate, post-check = 0, pre-check = 0 "); header (" Content-Type: $ file_type "); header (" Content-Length: $ file_size "); header (" content-Disposition: attachment; filename = \ "$ file_name \" "); header (" content-transfer-encoding: Binary "); readfile ($ file_path); $ file_type is the file type, $ file_name is the original name, and $ file_path is the address of the file stored on the service.

 

4. to sum up the cause of Garbled text, there are two possible causes for Garbled text. The first reason is the incorrect encoding (charset) settings, which causes the browser to parse the Garbled text with wrong encoding, as a result, "tianshu" is messy on full screen. Secondly, the file is opened with an incorrect code and saved. For example, a text file was originally gb2312 encoded, but opened and saved in UTF-8 encoding. To solve the above garbled code problem, you must first know which stages of development involve encoding:

 

1. file encoding: indicates the encoding of the page file (.html,. php, etc. Notepad and Dreamweaver automatically recognize the file encoding when opening the page, so there is no problem. Zendstudio does not automatically recognize the encoding, but it will only open the file with a certain encoding according to the preference configuration. If you do not pay attention during work, use the error code to open the file, after the modification, the garbled code will appear as soon as it is saved (I have a deep understanding ).

 

2. Page declarative encoding: in the HTML code head, you can use <meta http-equiv = "Content-Type" content = "text/html; charset = "XXX"/> 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, therefore, if English Symbols do not appear, Chinese characters are 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 rather than the 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 an adddefacharcharset gb2312 to the virtual machine in the configuration file to overwrite the global configuration, or configure it in the. htaccess directory. Conclusion: In a word, we need to solve the problem.MostPHP Chinese garbled charactersThe fastest solution 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"); XXX is the connection code. it will certainly solve the garbled problem.

Reprinted: http://www.cnblogs.com/showker/archive/2010/08/30/1812673.html

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.