_php tutorial on how to solve Chinese garbled characters in PHP

Source: Internet
Author: User
Tags mysql code mysql tutorial php and mysql
Chinese garbled solution in PHP tutorial
The most sleep is the Ajax post get Chinese garbled,

When passing Chinese with Ajax: FF will be smart enough to automatically convert Chinese characters to hexadecimal. BC's IE has to be handled by itself. -____!!

The simplest way is to use JS's built-in function where you need to escape encodeuricomponent

P.s. Was originally escaped in Chinese with escape. Notice the difference between it and encodeuricomponent.


Analysis of other garbled resolution methods in PHP
1. The encoding of the PHP file itself should match the encoding of the Web page

A. If you want to use gb2312 encoding, then PHP will output headers: header ("content-type:text/html; charset=gb2312 "), static page add, all files encoded in the format ANSI, can be opened with Notepad, save as select encoding for ANSI, overwrite source file.

B. If you want to use UTF-8 encoding, then PHP will output headers: header ("content-type:text/html; Charset=utf-8 "), static pages are added, all files are encoded in UTF-8 format. Save As Utf-8 may be a bit troublesome, general Utf-8 file will have a BOM at the beginning, if the use of the session will be a problem, can be saved EditPlus, in the EditPlus, tools---parameter selection, file->utf-8 signature, Select Always Delete, and then save the BOM information can be removed.

2. PHP itself is not Unicode, all functions such as substr should be changed to MB_SUBSTR (need to install mbstring extension), or transcoding with Iconv.

Two. PHP and MySQL tutorial data interaction

PHP and database tutorials should be coded in the same

1. Modify the MySQL configuration file My.ini or My.cnf,mysql best with UTF8 encoding

[MySQL]

Default-character-set=utf8

[Mysqld]

Default-character-set=utf8

Default-storage-engine=myisam

Under [Mysqld], add:

Default-collation=utf8_bin

init_connect= ' Set names UTF8 '

2. Add mysql_query ("Set Names ' code ') before PHP that needs to do the database operation, and the code is consistent with PHP code, if PHP code is gb2312 that MySQL code is gb2312, if it is utf-8 that MySQL code is UTF8, so the data is inserted or retrieved without garbled characters.

Three. PHP is related to the operating system

The encoding of Windows and Linux is not the same, in the Windows environment, the function of invoking PHP when the parameter is UTF-8 encoding will be error, such as Move_uploaded_file (), FileSize (), ReadFile (), etc. These functions are often used when handling uploads and downloads, and the following errors 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 ...

Warning:readfile () [Function.readfile]: Failed to open stream:invalid argument in.

In the Linux environment with GB2312 encoding although these errors will not occur, but the file name after the save is garbled resulting in unreadable files, you can first convert the parameters to the operating system identification code, encoding conversion can be used mb_convert_encoding (string, new code, the original code) or Iconv (original code, new Code, string), so that the file name stored after processing will not appear garbled, can also read the file, to achieve the Chinese name file upload, download.

In fact, there is a better solution, complete with the system out, also do not have to consider what the system is coding. Can generate a sequence of only letters and numbers as the file name, and the original with the Chinese name in the database, so call Move_uploaded_file () There is no problem, the download only need to change the file name to the original Chinese name. The code to implement the download 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 file type, $file _name is the original name, $file _path is the address of the file saved on the service

http://www.bkjia.com/PHPjc/632307.html www.bkjia.com true http://www.bkjia.com/PHPjc/632307.html techarticle in the PHP tutorial Chinese garbled solution is the most sleep is the Ajax post get Chinese garbled, using Ajax to pass the Chinese language. FF will be smart enough to automatically turn Chinese characters into hexadecimal. BC IE ...

  • 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.