Home > Backend Development > PHP Tutorial > Solution to Chinese garbled characters in php_PHP tutorial

Solution to Chinese garbled characters in php_PHP tutorial

WBOY
Release: 2016-07-13 10:54:45
Original
924 people have browsed it

Solution to Chinese garbled characters in php tutorial
The most annoying thing is that ajax post get has garbled Chinese characters,

When using ajax to transmit Chinese, ff will be very smart and automatically convert Chinese characters into hexadecimal. However, BC's ie has to handle it by itself..-____!!

The simplest way is to use js’s built-in function encodeuricomponent

where it needs to be escaped.

p.s. Initially, escape was used to escape Chinese characters. Pay attention to the difference between it and encodeuricomponent


Analysis of other solutions to garbled characters in PHP
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 should output the header: header("content-type: text/html; charset=gb2312"), add a static page, the encoding format of all files is ansi, and can be opened with Notepad , save as, select encoding as ansi, and overwrite the source file.

b. If you want to use utf-8 encoding, then php should output the header: header("content-type: text/html; charset=utf-8"), add the static page, and 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 tutorial

The coding of PHP and database tutorials 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 are 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 It is 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, the parameters can be converted to the encoding recognized by the operating system. The encoding conversion can be done with 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 the file can also be read normally, enabling uploading and downloading of Chinese name files. .

In fact, there is a better solution, which is completely separated 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

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 type of file, $file_name is the original name, $file_path is the address of the file saved on the service

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632307.htmlTechArticleIn the php tutorial, the most common solution to Chinese garbled characters is ajax post get. Chinese garbled characters are transmitted using ajax. When... ff will be very smart and automatically convert Chinese characters into hexadecimal. But bc's ie...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template