PHP Chinese garbled problem summary PHP Chinese garbled code is 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 of all, the best and fastest solution to PHP webpage encoding is to summarize the coding and PHP Chinese garbled characters of page declarations.
PHP Chinese garbled charactersIt is one of the common problems in PHP development.
PHP Chinese garbled charactersSometimes occurs on the web page, some occur in the MySQL interaction process, 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 ("Content-Type: text/html; charset = gb2312") and add it to the static page. The encoding format of all files is ANSI, which can be opened in Notepad. save it as ANSI and overwrite the source file. B. If you want to use UTF-8 encoding, php needs to output headers: header ("Content-Type: text/html; charset = utf-8") and add static pages 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 and OS-related Windows and Linux encoding are different,
In Windows, if the PHP function is called with UTF-8 encoding, errors may occur, such as move_uploaded_file (), filesize (), and readfile, 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 failed... in... warning: readfil E () [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 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