Why HTML web pages are garbled and how to solve them

php中世界最好的语言
Release: 2017-11-21 11:58:23
Original
3588 people have browsed it

Sometimes the web page we created is garbled when opened, so how do we solve this situation? The following will bring you the causes of garbled codes and methods to solve them.

1. Reasons for garbled codes

1. For example, the source code of the web page is encoded by gbk, and the Chinese characters in the content are encoded by utf-8. In this way, html garbled codes will appear when the browser is opened. . On the contrary, if the web page is encoded in utf-8 and the content is gbk, garbled characters will appear.

2. The encoding of the HTML web page is gbk, and the program calls out the content encoded in utf-8 from the database, which will also cause encoding garbled characters.

3. The browser cannot automatically detect the encoding of the web page, causing the web page to be garbled.

2. Methods to solve garbled characters

The first one is that the source code encoding of the html web page is different from the Chinese character input encoding.

Solution:

Use software to edit HTML web page content. It is recommended to use DW software for HTML code editing and development.

Try not to use Notepad directly to edit HTML code.

Second, if the web page setting encoding is gbk, and the database storage data encoding format is UTF-8, then the program queries the database data and displays the data before entering the native program for transcoding.

For example, PHP program + mysql query display data transcoding:

1. mysql_query("SET NAMES 'UTF8'"); //Transcode the query data to utf8, that is, convert it to utf-8

2, mysql_query("SET NAMES 'GBK'");//Transcode the query data to GBK, such as gbk2312

When writing the database connection file, write:

$conn = mysql_connect("$host","$user","$password"); 
mysql_query("SET NAMES 'UTF8'"); 
mysql_select_db("$database",$conn);
Copy after login

Then when making the page, pay attention to this sentence:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

In this way, regardless of the Chinese input into the database or the page display, everything will be normal.

In the DW CS4 version, the utf8 page is also generated by default.

Similarly, if you write the database connection file at the beginning as:

mysql_query("SET NAMES &#39;GBK&#39;");
Copy after login

, then the page should also become:

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
Copy after login
Copy after login

Other asp programs or other programming language websites are based on For the actual situation, go to Baidu to check the conversion encoding method.

The third type is that the browser causes garbled characters.

This may be caused by your web page not setting meta charset encoding. As a result, the browser cannot recognize the default encoding type of your web page. Solution:

1. If the web page is garbled when browsing in the browser, find the menu to convert the encoding in the browser.

In IE9 browser, right-click the blank page of the webpage that needs to be transcoded and select "Encoding".

When browsing web pages that need to be transcoded in Maxthon Browser, menu "View"-->"Encoding" to select the conversion encoding

When browsing web pages that need to be transcoded in Google Chrome , click the "three horizontal" icon in the upper right corner and select "Tools" --> "Encoding" to choose to switch the web page encoding so that the browser will not browse this web page with garbled characters.


#2. If you develop your own web page, you must add meta charset encoding tags to the web page.

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
Copy after login
Copy after login


or

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Copy after login

If the conversion is not completed by adding the meta charset encoding tag in Notepad, this will also cause garbled characters. That is because the direct After Notepad adds or modifies the encoding format, the content of the corresponding Html hypertext document does not change with the addition or modification of the encoding format. At this time, true transcoding is required, so it is best to use development software to add and modify encoding.

It is recommended to modify the encoding in DW software. Modify or add the encoding in the DW software and reload the encoded web page.

Friends who need the solution to garbled web pages can save it, and please continue to pay attention to other updates on this site.

Related reading:

How to use checkbo in HTML


Naming rules in HTML


How to modify the font color in html

The above is the detailed content of Why HTML web pages are garbled and how to solve them. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!