PHP character set encoding garbled solution _ PHP Tutorial

Source: Internet
Author: User
Tags ultraedit
PHP character set encoding garbled solution. Either the original Chinese characters on the page and the Chinese characters retrieved from the database are garbled; or the original Chinese characters and database Chinese characters are displayed normally, and the other is garbled. Very annoying!Either the original Chinese characters on the page and the Chinese characters retrieved from the database are garbled; or the original Chinese characters and database Chinese characters are displayed normally, and the other is garbled. Very annoying! The problem must be solved step by step. Before you perform the following operations, you need to configure the Web server to integrate it with PHP to debug the PHP program. We take the common GB2312 and UTF-8 character set as examples to test and explain. The browser is IE7.0.

Solution to garbled original Chinese characters on the page

I will not talk about the principles of PHP character set encoding. I will search for the character string "PHP garbled" on the Internet, and the entire box of articles will be provided for you to read. I am most concerned about how to solve this problem. My favorite text editor is UltraEdit. it not only supports hexadecimal editing, but also supports multi-encoding documents. To solve this problem, use the UltraEdit function.

Open Chinese Windows, use UltraEdit to create a text file, and manually enter a PHP page file. the file content is as follows. Save as the test1.php file. select "default" in the "format" drop-down box.

 
 
  1. < Html>
  2. < Head>
  3. < Title>Page title</Title>
  4. < META Http-equiv=Content-TypeContent="Text/html; charset = gb2312">
  5. </Head>
  6. < Body>
  7. Computer Learning Network:< Br>
  8. </Body>
  9. </Html>

Open this page of the website in IE browser. As you can see, the page is displayed normally. In the IE browser's "View"/"encoding" menu, you can see (check "automatically selected"). The character encoding is GB2312.

[Display normally in Firefox 2.0.]

Then, under the files menu of UltraEdit, select save as, select UTF-8 in the format, and the file name is test2.php. Open this page in IE browser. As you can see, the page is displayed normally (in fact, the English font has changed slightly ). In IE browser "View"/"encoding" menu to see (checked "automatically selected"), character encoding is UTF-8, automatically changed! Note that the sentence is not modified, but the browser automatically identifies the real PHP character set encoding! It seems that IE is still relatively smart. it also means that the automatic identification character set of IE is better than the definition of charset = xxx in METE labels.

[Garbled characters are displayed in Firefox 2.0.]

Add a statement to the first part of the page

 
 
  1. php
  2. header("Content-Type:text/html;
  3. charset=utf-8");
  4. ?>

Save the page file again. select "default" in the "format" drop-down box and the file name is test3.php. Use IE to open the file on the website. this time, after English letters are removed, Chinese characters are garbled! At the same time in IE browser "View"/"encoding" menu to see (checked "automatically selected"), character encoding is UTF-8, has been forcibly changed.

The reason for Chinese characters garbled, because the original GB2312 encoding, is forced to display in the form of UTF-8 encoding, so garbled appear. At this time, GB2312 encoding is specified in the browser, and the Chinese characters on the page are displayed normally. (this is not the case when a page is created. you have to select the PHP character set encoding for the browser, one is that the viewer may not know how to select the encoding and the encoding. In addition, it seems that we are too good !).

[Garbled characters are displayed in Firefox 2.0.]

Add a statement to the first part of the page

 
 
  1. < ?php
  2. header("Content-Type:text/html;
  3. charset=GB2312");
  4. ?>

Save the page file again, select UTF-8 in the format drop-down box, and the file name is test4.php. Use IE to open the file on the website. Strange: The Chinese characters displayed on the page are normal, not unexpected garbled characters ?! The PHP character set encoding is still a UTF-8 and is not forcibly changed to the GB2312 character set, as you can see under the View/encoding menu in IE browser (select automatically.

At this time, GB2312 encoding is specified manually in the browser, and it is found that the IE browser cannot specify the encoding manually. It seems that IE browser is especially concerned about the character set of UTF-8. No matter the META tag or PHP statement, Chinese characters cannot be garbled in IE.

[Garbled characters are displayed in Firefox 2.0.]

Summary: the above tests are mainly performed under IE7.0. the Web Server is IIS6.0 under Windows Server 2003, and the PHP version is 4.4.7. We can see that IE7.0 has done a lot of automatic processing to correctly identify the character set to show its intelligence and friendliness. Sometimes we are overwhelmed by diligence. Chinese character garbled characters are related to different browsers, different versions, Web servers, background scripts, and different character sets. As a Web programmer, you can focus on factors related to yourself. you do not need to become an expert in PHP character set encoding. To be compatible with popular IE and FF browsers, we can handle our PHP code in the following simple ways:

1. the real character set of the page should be the same as that specified by the META tag;

2. you can also use the header ("Content-Type: text/html; charset = xxx"); statement to specify the character set, but it cannot conflict with the real character set, it cannot conflict with the META tag. (Although the test results show that when header () conflicts with META, header () takes precedence over the character set specified by META, because it is displayed according to HttpWatch Basic trace, header () after the character set is specified, the IE browser Type will be explicitly specified by the character set. But it cannot be guaranteed that other non-mainstream browsers will do the same .)

3. PHP character set encoding cannot conflict with the character set retrieved from the database. Otherwise, the page may contain Chinese characters on the page and the Chinese characters retrieved from the database. all the characters are garbled or partially garbled.


Bytes. Very annoying !...

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.