Foreword
PHP Chinese garbled characters are one of the common problems in PHP development. Chinese garbled characters sometimes occur in the web page itself, some occur in the process of MYSQL interaction, and sometimes are related to the operating system. The following is a summary.
1. Encoding of PHP web pages
The best and fastest solution is to make the encoding declared by the page consistent with the internal encoding of the database. If the encoding requested by the page is inconsistent with the encoding within the database, then set the connection encoding. Mysql_query(“set names ***”).
1. The encoding of the php file itself should match the encoding of the web page. If you want to use gb2312 encoding, then php must output the header
header(“Content-type:text/html;charset=gb2312”)
Static page addition
, the encoding format of all files is ANSI, you can open it with Notepad, save as, select the encoding as ANSI, and overwrite the source file.
2. If you want to use uft-8 encoding, then PHP needs to output the header
header(“Content-type:text/html; charset=utf-8”),
Static page addition
, the encoding format of all files is utf-8. Saving as utf-8 may be a bit troublesome, generally utf There will be BOM at the beginning of -8. If you use Session, there will be problems. You can use editplus software to save it. In this software, select Tools>Parameters>File>UTF-8 Signature, select 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 needs to be installed); or iconv can be used to transcode.
Add mysql_query ("set names encoding") before the PHP program that needs to perform database operations. The encoding is consistent with the PHP encoding. If the PHP encoding is gb2312, then the mysql encoding is gb2312. If it is uft-8, then the mysql encoding is utf8. After this change, garbled characters will not appear.