Solutions to Chinese garbled characters in php

Source: Internet
Author: User
Tags mysql code
The most obvious thing is that ajaxpostget garbled Chinese characters. when using ajax to deliver Chinese characters, ff will be very smart and automatically convert Chinese characters into hexadecimal characters, while bc's ie has to handle it by itself -____!!

The most obvious thing is that ajax post get is garbled in Chinese .. ff automatically converts Chinese characters to hexadecimal notation. while bc's ie has to handle it on its own .. -____!!

The simplest method is to use the built-in function encodeuricomponent of js where escape is needed. at first, it uses escape for Chinese escape .. note the difference between it and encodeuricomponent.

I. Analysis of other solutions that cause garbled characters in php

1. the php file encoding should match the webpage encoding.

A. if you want to use gb2312 encoding, php needs to output the header ("content-type: text/html; charset = gb2312") and add it to the 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. if you want to use UTF-8 encoding, php needs to output headers: header ("content-type: text/html; charset = utf-8") and add 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, always delete, and then save it 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.

II. data interaction between php and mysql

Php and database tutorial encoding should be consistent

1. modify the mysql configuration file my. ini or my. cnf. it is best to use utf8 encoding for mysql.

  1. [Mysql]
  2. Default-character-set = utf8
  3. [Mysqld]
  4. Default-character-set = utf8
  5. Default-storage-engine = myisam
  6. Add the following under [mysqld:
  7. Default-collation = utf8_bin
  8. 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 often used for processing uploads and downloads. the following errors may occur during calls:

  1. Warning: move_uploaded_file () [function. move-uploaded-file]: failed to open stream: invalid argument in...
  2. Warning: move_uploaded_file () [function. move-uploaded-file]: unable to move ''to ''in...
  3. Warning: filesize () [function. filesize]: stat failed for... in...
  4. 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 with only letters and numbers as the file name, and store the original Chinese name in the database. in this way, calling move_uploaded_file () will not cause problems, during the download, you only need to change the file name to the original name with Chinese characters. The download code is as follows:

  1. Header ("pragma: public ");
  2. Header ("expires: 0 ");
  3. Header ("cache-component: must-revalidate, post-check = 0, pre-check = 0 ");
  4. Header ("content-type: $ file_type ");
  5. Header ("content-length: $ file_size ");
  6. Header ("content-disposition: attachment; filename =" $ file_name "");
  7. Header ("content-transfer-encoding: binary ");
  8. 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.


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.