Php Chinese garbled classification and solutions

Source: Internet
Author: User
PHP + MYSQL for website development usually encounters garbled characters when the browser outputs Chinese characters. this problem is mainly caused by HTML content encoding. PHP + MYSQL for website development usually encounters garbled characters when the browser outputs Chinese characters, this problem is mainly caused by inconsistent HTML content encoding, php file encoding, and MySQL database encoding. Next we take UTF-8 as an example to briefly describe how to unify the relationship between the three.

Add a php file named test_charset.php and save the following code to the file:

             
         Hello,
 ! Your email is: ">
     

HTML content encoding

The above row 22nd:

 

Here we specify that the browser uses UTF-8 encoding when parsing HTML. If this parameter is not specified, the browser uses its own default encoding. The default encoding varies with browsers, for example, IE6 is GB2312 and FireFox is UTF-8. Therefore, if there are no 22nd lines of the above code, the code will be displayed normally in FireFox and garbled characters will be displayed in IE6.

Php file encoding

Php files also need to be encoded in the same way. How can I check the encoding of my php file? In Windows, Notepad can be used for processing. Open the php file in Notepad and select "Save As..." from the "file" menu, for example:

There is an encoding option at the bottom of the displayed dialog box. The current encoding of the file is displayed. To change to another encoding, select from the drop-down list box and click "save.

MySQL database encoding

After the database connection is successful, you should immediately execute a set encoding command, such as the first line of the above code. Here a little need to pay special attention, UTF-8 is a normal way of writing, but in MySQL into UTF8, there is no crossline in the middle. The above 7th lines set the connection encoding to utf8 ($ charset = "utf8") instead of UTF-8. The complete implementation of ECShop connection encoding is attached for your reference. The file is des/cls_mysql.php.

Function set_mysql_charset ($ charset) {/* if the mysql version is 4.1 + or later, you need to initialize the character set */if ($ this-> version> '4. 1 ') {if (in_array (strtolower ($ charset), array ('gbk', 'big5', 'utf-8', 'utf8 '))) {$ charset = str_replace ('-', '', $ charset);} if ($ charset! = 'Latin1') {mysql_query ("SET character_set_connection = $ charset, character_set_results = $ charset, character_set_client = binary", $ this-> link_id );}}}

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.