Home > Backend Development > PHP Tutorial > Solution to garbled characters in PHP character set encoding_PHP Tutorial

Solution to garbled characters in PHP character set encoding_PHP Tutorial

WBOY
Release: 2016-07-15 13:30:35
Original
958 people have browsed it

Either the original Chinese characters on the page and the Chinese characters taken out from the database are all garbled; or one of the original Chinese characters and the Chinese characters in the database is displayed normally, while the other is garbled. Very annoying and irritating! The problem needs to be solved step by step. Before actually following the methods below, you need to configure your web server to integrate with PHP so that you can finally debug your PHP program. We take the common GB2312 and UTF-8 character sets as examples to test and illustrate. The browser is IE7.0.

Solution to the garbled original Chinese characters on the page

I won’t go into details about the principle of PHP character set encoding. Search the Internet for the string "PHP garbled code", and the whole frame will be full. articles for everyone to read. What I am most concerned about is how to solve this practical problem. My favorite text editor to use is UltraEdit, not only because it can do hex editing, but also because it supports multi-encoding documents. To solve this problem, you need to use this feature of UltraEdit.

Open Chinese Windows, use UltraEdit to create a text file, and manually enter a PHP page file. The file content is as follows. Save it as a test1.php file. Note that when saving, select "Default" in the "Format" drop-down box - pay special attention here.

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</SPAN><SPAN> </SPAN><SPAN class=tag-name>Html</SPAN><SPAN class=tag>></span><span> </span></span></li>
<li>
<span class="tag"><</SPAN><SPAN> </SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span><span> </span>
</li>
<li class="alt">
<span class="tag"><</SPAN><SPAN> </SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span><span>页面标题</span><span class="tag"><</SPAN><SPAN> /title</SPAN><SPAN class=tag>></span><span> </span>
</li>
<li>
<span class="tag"><</SPAN><SPAN> </SPAN><SPAN class=tag-name>META</SPAN><SPAN> </SPAN><SPAN class=attribute>http-equiv</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>Content</SPAN><SPAN>-Type </SPAN><SPAN class=attribute>content</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"text/html; charset=gb2312"</SPAN><SPAN class=tag>></span><span> </span>
</li>
<li class="alt">
<span class="tag"><</SPAN><SPAN> /head</SPAN><SPAN class=tag>></span><span> </span>
</li>
<li>
<span class="tag"><</SPAN><SPAN> </SPAN><SPAN class=tag-name>Body</SPAN><SPAN class=tag>></span><span> </span>
</li>
<li class="alt">
<span>电脑学习网:</span><span class="tag"><</SPAN><SPAN> </SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span><span> </span>
</li>
<li>
<span class="tag"><</SPAN><SPAN> /body</SPAN><SPAN class=tag>></span><span> </span>
</li>
<li class="alt">
<span class="tag"><</SPAN><SPAN> /html</SPAN><SPAN class=tag>></span><span> </span>
</li>
</ol>
Copy after login

Open this page of the website with IE browser. As you can see, the page displays normally. Under the "View"/"Encoding" menu of the IE browser ("Automatic selection" is checked), the character encoding is GB2312.

[Displays normally under Firefox 2.0. ]

Then, under the "File" menu of UltraEdit, select "Save As", select "UTF-8" as the format, and the file name is test2.php. Open this page with IE browser. As you can see, the page displays normally (in fact, the English font has changed slightly). I saw under the "View"/"Encoding" menu of the IE browser ("Automatic selection" was checked) that the character encoding was UTF-8, which changed automatically! Note: This sentence has not been modified, but the browser automatically recognizes the real PHP character set encoding! It seems that IE is quite smart, which also shows that IE's automatic identification of character sets takes precedence over the definition of charset=xxx in the METE tag.

[Garbled characters are displayed under Firefox 2.0. ]

Add the sentence

<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li>
<li><span>header("Content-Type:text/html;</span></li>
<li>
<span class="attribute">charset</span><span>=</span><span class="attribute-value">utf</span><span>-8");  </span>
</li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

Save the page file again, and select "Default" in the "Format" drop-down box. The file name is test3.php. Use IE to open the file on the website, and this time I see that in addition to the English letters, the Chinese characters have become garbled! At the same time, I saw under the "View"/"Encoding" menu of the IE browser ("Automatic selection" was checked) that the character encoding is UTF-8, which has been forcibly changed.

The reason why Chinese characters are garbled is because the original GB2312 encoding is forced to be displayed in UTF-8 encoding, so garbled characters appear. At this time, the GB2312 encoding is manually specified in the browser, and the Chinese characters on the page are displayed normally again (this cannot be done when actually creating the page. The viewer must choose the PHP character set encoding. One is that the viewer may not know how to choose at all. Encoding, what encoding to choose, and it seems that we are too good!).

[Garbled characters are displayed under Firefox 2.0. ]

Add the sentence

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</SPAN><SPAN> ?php  </SPAN></SPAN><LI><SPAN>header("Content-Type:text/html;</SPAN><LI><SPAN class=attribute>charset</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>GB2312</SPAN><SPAN>");  </SPAN><LI class=alt><SPAN class=tag>?></span><span> </span></span></li></ol>
Copy after login

Save the page file again, and select "UTF-8" from the "Format" drop-down box ”, the file name is test4.php. Use IE to open the file on the website, and it's strange: I see that the Chinese characters on the page are displayed normally, not the expected garbled characters? ! Under the "View"/"Encoding" menu of the IE browser ("Automatic selection" is checked), the PHP character set encoding is still UTF-8 and has not been forcibly changed to the GB2312 character set.

At this time, I manually specified the GB2312 encoding in the browser and found that the IE browser could not manually specify the encoding. It seems that the IE browser pays special attention to the UTF-8 character set. Regardless of whether it is specified in the META tag or the PHP statement, the IE browser cannot display garbled Chinese characters.

[Garbled characters are displayed under Firefox 2.0. ]

To summarize: The above tests were mainly conducted under IE7.0, the Web server was IIS6.0 under Windows Server 2003, and the PHP version was 4.4.7. It can be seen that IE7.0 has done a lot of additional automatic processing work in order to correctly identify the character set to show its intelligence and friendliness. Sometimes being too diligent can overwhelm us. Since the problem of garbled Chinese characters is related to different browsers and their different versions, web servers, background scripts and different character sets, the problem is particularly complicated. As a web programmer, you can focus mainly on factors related to yourself. There is no need to become an expert in PHP character set encoding. In order to be compatible with the currently popular IE and FF browsers, we can process our PHP code in the following simple ways:

1. The real character set of the page should be consistent with 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 of the character, nor with the META tag. (Although test results show that when header() conflicts with META, header() takes precedence over the character set specified by META, because according to HttpWatch Basic tracking, after header() specifies the character set, the IE browser Type will be clear Get character set specification, but there is no guarantee that other non-mainstream browsers will do the same. )

3. The PHP character set encoding cannot conflict with the character set of the characters retrieved from the database. Otherwise, the page will have the problem of all or part of the Chinese characters being garbled and the Chinese characters retrieved from the database being garbled.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446271.htmlTechArticleEither the original Chinese characters on the page and the Chinese characters taken out from the database are all garbled; or the original Chinese characters and the database Chinese characters are displayed together. If it is normal, the other one will become garbled. Very annoying and irritating! ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template