Perfect solution for PHP Chinese garbled characters

Source: Internet
Author: User
Tags mysql code
Chinese Garbled text is a very tricky issue when using jsp. Recently, I want to write a project, and I think it is necessary to solve Chinese Garbled text. So google made a small summary and shared it with you. Do not spray old birds .... I. First, PHP webpage encoding 1. php file encoding should match the webpage encoding a. If you want to use gb2312 encoding, PHP should enter

Chinese Garbled text is a very tricky issue when using jsp. Recently, I want to write a project, and I think it is necessary to solve Chinese Garbled text. So google made a small summary and shared it with you. Do not spray old birds .... I. First, PHP webpage encoding 1. php file encoding should match the webpage encoding a. If you want to use gb2312 encoding, PHP should enter

Chinese Garbled text is a very tricky issue when using jsp. Recently, I want to write a project, and I think it is necessary to solve Chinese Garbled text. Google,
I made a summary and shared it with you. Do not spray old birds ....


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 The encoding format of all files is ANSI, which can be opened in Notepad. Save it as ANSI and overwrite the source file.
B. To use UTF-8 encoding,

Php needs to output header: header ("Content-Type: text/html; charset = UTF-8 "),
Add html 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 always 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.
For example echo iconv ('gb2312', 'utf-8', $ str); // encode the string from GB2312 to UTF-8


2. Data Interaction between PHP and Mysql
PHP and database encoding should be consistent
1. Modify the mysql configuration file my. ini or my. cnf. It is best to use utf8 encoding for mysql.
[Mysql]
Default-character-set = utf8
[Mysqld]
Default-character-set = utf8
Default-storage-engine = MyISAM
Add the following 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 related to the Operating System
The encoding for Windows and Linux is different. In Windows, if the parameter is UTF-8 encoded when a function of PHP is called, an error occurs, such as move_uploaded_file (), filesize (), and readfile () these functions are processing uploads,DownloadThe following errors 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 ''to ''in...
Warning: filesize () [function. filesize]: stat failed for... 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 containing only letters and numbers as the file name, and save the original Chinese name inDatabaseIn this way, calling move_uploaded_file () will not cause problems. during 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.

Note: difference between UTF-8 and UTF-8 without BOM: Byte Order Mark
UTF-8 BOM is also called the UTF-8 signature, in fact, the BOM of the UTF-8 has no effect on the UFT-8, is to support the UTF-16, the BOM is added to the UTF-32, the BOM signature is to tell the editor of the current file using what encoding,
Convenience editor identification, but BOM although not displayed in the editor, but will produce output, just like an empty line. With BOM UTF-8, allPHPUnrecognized.

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.