Home >Backend Development >PHP Tutorial >How to deal with common Chinese garbled characters in PHP development_PHP tutorial
During the development of PHP and MySQL, Chinese garbled characters will appear if you are not careful. This is a problem of database encoding and file encoding. Below we have compiled a large number of solutions to Chinese garbled characters in PHP.
PHP Chinese garbled characters sometimes occur on the web page itself, some occur during the interaction with MySQL, and sometimes are related to the operating system. Here is a summary.
1. The first is the encoding of the PHP web page
The best and fastest solution is to make the encoding declared by the page consistent with the internal encoding of the database. If the page number applied for the page is inconsistent with the internal encoding of the database, set the connection encoding,
代码如下 | 复制代码 |
mysql_query("SET NAMES XXX "); |
XXX is the connection encoding. It will definitely solve the problem of garbled characters.
1. The encoding of the php file itself and the encoding of the web page should match a. If you want to use gb2312 encoding, then php must output the header:
代码如下 | 复制代码 |
header(“Content-Type: text/html; charset=gb2312"), |
Static page added
代码如下 | 复制代码 |
, |
The encoding format of all files is ANSI, which can be opened with Notepad, save as and select the encoding as ANSI to overwrite the source file. b. If you want to use utf-8 encoding, then php needs to output the header:
代码如下 | 复制代码 |
header(“Content-Type: text/html; charset=utf-8"), |
Static page added
代码如下 | 复制代码 |
, |
The encoding format of all files is utf-8. Saving as utf-8 may be a bit troublesome. Generally, utf-8 files will have BOM at the beginning. If you use session, there will be problems. You can use editplus to save. In editplus, go to Tools->Parameter Selection->File-> UTF-8 signature, select Always delete, 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 needs to be installed); or iconv can be used to transcode.
2. Data interaction between PHP and Mysql
The encoding of PHP and database 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 under [mysqld]:
代码如下 | 复制代码 |
default-collation=utf8_bin init_connect='SET NAMES utf8' |
2. 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 UTF-8, then The mysql encoding is utf8, so that there will be no garbled characters when inserting or retrieving data
3. PHP is related to the operating system. The encoding of Windows and Linux is different. In the Windows environment, if the parameters are utf-8 encoded when calling PHP functions, errors will occur, such as move_uploaded_file(), filesize(), readfile(), etc. These functions are often used when processing uploads and downloads. The following error may occur when calling: 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 these errors will not occur when using gb2312 encoding in a Linux environment, the saved file name will be garbled and the file cannot be read. In this case, you can first The parameters are converted into the encoding recognized by the operating system. The encoding conversion can be mb_convert_encoding (string, new encoding, original encoding) or iconv (original encoding, new encoding, string), so that the file name saved after processing will not be garbled, and Files can be read normally, and files with Chinese names can be uploaded and downloaded. In fact, there is a better solution, which is to completely separate from the system, and there is no need to consider the encoding of the system. You can generate a sequence of only letters and numbers as the file name, and save the original name with Chinese characters in the database. In this way, there will be no problem when calling move_uploaded_file(). When downloading, you only need to change the file name to the original name with Chinese characters. Chinese name.
The code to implement downloading is as follows
The code is as follows | Copy code | ||||
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 type of file, $file_name is the original name, and $file_path is the address of the file saved on the service.
|
1. File encoding: refers to the encoding in which the page file (.html, .php, etc.) itself is saved. Notepad and Dreamweaver will automatically recognize the file encoding when opening the page, so there will be less problems. However, ZendStudio does not automatically recognize the encoding. It will only open the file in a certain encoding according to the configuration of the preferences. If you accidentally open the file with the wrong encoding while working, and save it after making the modification, garbled characters will appear ( I feel it deeply).
代码如下 | 复制代码 |
to tell the browser what encoding is used for the web page. Currently, XXX mainly uses GB2312 and UTF-8 in Chinese website development. 3. Database connection encoding: refers to the encoding used to transmit data to the database when performing database operations. It should be noted here that it should not be confused with the encoding of the database itself. For example, the default encoding within MySQL is latin1, which means that Mysql uses latin1. Encoding to store data, data transmitted to Mysql in other encodings will be converted to latin1 encoding. Knowing where coding is involved in WEB development, you also know the reasons for garbled codes: the above three coding settings are inconsistent. Since most of the various codings are compatible with ASCII, English symbols will not appear, and Chinese characters will be unlucky. .
5. Fight some common error situations and solutions:
1. The database uses UTF8 encoding, and the page declaration encoding is GB2312. This is the most common cause of garbled characters. At this time, the direct SELECT data in the PHP script will be garbled, and you need to use it before querying:
代码如下 | 复制代码 |
mysql_query("SET NAMES GBK"); |
To set the MYSQL connection encoding, ensure that the page declaration 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 commonly used UTF-8. If the encoding of the page declaration is consistent with the internal encoding of the database, you do not need to set the connection encoding. Note: In fact, the data input and output of MYSQL is more complicated than what is mentioned above. There are 2 default encodings defined in the MYSQL configuration file my.ini, which are default-character-set in [client] and default in [mysqld] -character-set to set the encoding used by default for client connections and internal databases respectively. The encoding we specified above is actually the command line parameter character_set_client when the MYSQL client connects to the server, which tells the MYSQL server what encoding the client data received is, instead of using the default encoding.
2. The page declaration encoding is inconsistent with the encoding of the file itself. This rarely happens because if the encoding is inconsistent, what the artist sees in the browser when creating the page will be garbled characters. More often than not, it is caused by fixing some minor bugs after release, opening the page in the wrong encoding and then saving it. Or you use some FTP software to directly modify files online, such as CuteFTP. Due to incorrect software encoding configuration, the wrong encoding is converted. 3. Some friends who rent virtual hosts still have garbled characters even though the above three encodings are set correctly. For example, if the web page is encoded in GB2312, it is always recognized as UTF-8 when opened by browsers such as IE. The HEAD of the web page has already stated that it is GB2312. After manually changing the browser encoding to GB2312, the page displays normally. The reason is that the server Apache sets the global default encoding of the server and adds AddDefaultCharset UTF-8 in httpd.conf. At this time, the server will first send the HTTP header to the browser, and its priority is higher than the encoding declared in the page. Naturally, the browser will recognize it incorrectly. There are two solutions. Administrators should add AddDefaultCharset GB2312 to their own virtual machine in the configuration file to override the global configuration, or configure it in .htaccess in their own directory. Summary: In a word, the best and fastest way to solve the most garbled Chinese PHP code is to make the encoding declared on the page consistent with the internal encoding of the database. If the page number applied for the page is inconsistent with the internal encoding of the database, set the connection encoding.
代码如下 | 复制代码 |
mysql_query("SET NAMES XXX "); |
XXX is the connection encoding. It will definitely solve the problem of garbled characters.