PHP Chinese garbled text is one of the common problems in PHP programming. Chinese garbled text sometimes occurs on the web page, and some occur in the MYSQL interaction process, sometimes related to the operating system.
PHP Chinese garbled text is one of the common problems in PHP programming. Chinese garbled text sometimes occurs on the web page. Some are generated during MYSQL interaction and sometimes related to the operating system. The following is a summary.
I. PHP source code
The best and fastest solution is to make the page declarative encoding consistent with the internal encoding of the database. if the requested encoding is inconsistent with the internal encoding of the database, set the connection encoding. Mysql_query ("set names ***").
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 static pages
The encoding format of all files is ANSI, which can be opened in Notepad. save it as ANSI and overwrite the source file.
2. if you want to use uft-8 encoding, php needs to output the header
Header ("Content-type: text/html; charset = utf-8 "),
Add static pages
The encoding format of all files is utf-8. it may be a little troublesome to save it as UTF-8. BOM is usually started with UTF-8. if you use Session, problems may occur. you can use editplus to save it. In this software, select a tool? Parameter selection? File? UTF-8 signature, choose always delete, and then save to remove the BOM information.
3. 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.
Add mysql_query ("set names encoding") before the PHP program that requires database operations. the encoding is consistent with the PHP code. if the PHP code is gb2312, then the mysql code is gb2312, if it is uft-8, then mysql is utf8. so after the change will not appear garbled.
From battlefield diaries _ LAMP brothers