Either the original characters of the page and the Chinese characters taken from the database are all garbled; either the original Chinese characters and the database characters, one shows normal, the other becomes garbled. Very annoying very irritating! The problem requires a step-by-step solution. Before you actually do the following, you need to configure your WEB server to integrate with PHP and eventually debug your PHP program. We test and illustrate the common GB2312 and UTF-8 character sets as an example. The browser is IE7.0.
The solution of the original characters garbled in the page
The principle of PHP character set encoding I will not say, online search for the string "PHP garbled", the whole frame of the article for everyone to read. I am most concerned about the specific how to do, can solve this practical problem. My favorite text editor is UltraEdit, not just because it can be edited in 16, but also because it supports multiple encoded documents. Solving this problem requires the use of the UltraEdit feature.
Open Chinese Windows, create a text file with UltraEdit, manually enter a PHP page file, the file content is as follows. Save As test1.php file, note Save as "format" drop-down box select "Default"-pay special attention here.
- < Html >
- < head >
- < title > page title < /title>
- < META http-equiv=Content -type Content="text/html; charset=gb2312 ">
- < /head >
- < Body >
- Computer Learning Network: < br >
- < /body >
- < /html >
Use IE browser to open this page of the website. As you can see, the page appears normal. In IE, under the "View"/"Encode" menu, see (tick "Auto Select"), character encoding is GB2312.
[Firefox 2.0 shows normal.] ]
Then, under UltraEdit's File menu, choose Save As, format select UTF-8, and the file name is test2.php. Open this page in IE browser. As you can see, the page is displayed correctly (in fact, the English font has changed slightly). In IE Browser under the "View"/"Encoding" menu See (tick "Auto Select"), character encoding is UTF-8, automatically changed! Note that there is no modification, but the browser has automatically identified the real PHP character set encoding! It seems that IE is still relatively clever, also shows that IE automatically discriminate character set is more than the definition of charset=xxx in the mete tag priority.
[Firefox 2.0 appears garbled. ]
Add a statement to the header of the page
- Php
- Header ("content-type:text/html;
- CharSet = UTF -8 ");
- ?>
Save the page file again, the Format drop-down box select default, and the file name is test3.php. Use IE to open the site of the file, this time to see, in addition to the English alphabet, Chinese characters have become garbled! At the same time, under the "View"/"Encode" menu of IE browser (tick "Auto Select"), character encoding is UTF-8, has been forced to change.
The reason why Chinese characters garbled, is because the original GB2312 code, is forced to UTF-8 encoding form display, so garbled appear. This time in the browser person for the specified GB2312 code, the page character is also displayed normal (the real page is not to do so, not to let the viewer choose PHP Character set encoding, one is that the viewer may not know how to choose the code, choose what code, and also seems we too food! )。
[Firefox 2.0 appears garbled. ]
Add a statement to the header of the page
- < ? PHP
- Header ("content-type:text/html;
- CharSet = GB2312 ");
- ?>
Save the page file again, and the Format drop-down box selects "UTF-8" and the file name is test4.php. Use IE to open the Web site of the file, strange: See the page Chinese characters display normal, not pre-imagined garbled?! The PHP character set encoding is still UTF-8 and is not forced to change the GB2312 character set, as seen under the "View"/"Encode" menu of IE browser (tick "auto Select").
This time in the browser people for the specified GB2312 code, found that IE browser does not specify the encoding. It seems that IE browser has a special care for the UTF-8 character set. Neither the META tag designation, nor the PHP statement designation, will allow IE browser to display characters garbled.
[Firefox 2.0 appears garbled. ]
Summary: The above test is mainly under IE7.0, the WEB server for Windows Server 2003 under the iis6.0,php version of 4.4.7. As you can see, IE7.0 has done a lot of extra automatic work to show its intelligence and friendliness in order to correctly identify the character set. Sometimes being too attentive makes us at a loss. The problem is particularly complicated by the fact that the problem with Chinese characters is somewhat related to different browsers and different versions, WEB servers, background scripts, and different character sets. As a WEB programmer, it is important to focus on the factors that are relevant to you, and there is no need to be an expert in PHP character set coding. In order to be compatible with the current popular IE and FF browsers, we can process our PHP code in the following simple way:
1, the true character set of the page should be consistent with the META tag designation;
2, you can also use the header ("content-type:text/html;charset=xxx"); statement specifies a character set, but cannot conflict with a character's true character set, nor can it conflict with a META tag. (although according to the test results, the header () is more preferred than the meta-specified character set when the header () and the meta conflict, because the HttpWatch Basic trace shows that after the header () specifies the character set, the IE browser Type will explicitly get the character Set specified. But there is no guarantee that other non-mainstream browsers will do the same. )
The 3,php character set encoding cannot conflict with the character set retrieved by the database, otherwise the page will appear on the page itself and the Chinese characters retrieved by the database, all garbled or partially garbled problems.
http://www.bkjia.com/PHPjc/446271.html www.bkjia.com true http://www.bkjia.com/PHPjc/446271.html techarticle either the original characters of the page and the Chinese characters taken from the database are all garbled; either the original Chinese characters and the database characters, one shows normal, the other becomes garbled. Very annoying very irritating! ...