There are three reasons that cause html web pages to be garbled. So today we will talk about three different causes and solutions to the three problems.
The cause of garbled HTML web pages is mainly caused by the difference between the Chinese text content in the HTML source code and the HTML encoding. But no matter which situation causes garbled code, the web page encoding needs to be set at the beginning of the web page.
Causes of 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 case, 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.
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, at this time the program queriesthe database The data display data can be transcoded before entering the local program.
For example, PHP program + mysql query display data transcoding:
1, mysql_query("SET NAMES 'UTF8'"); //Transcode the query data to utf8, that is, converted to utf-8
2, mysql_query("SET NAMES 'GBK'");//Transcode the query data to GBK, such as gbk2312
when writing the database connection When writing the file, write:
$conn = mysql_connect("$host","$user","$password"); mysql_query("SET NAMES 'UTF8'"); mysql_select_db("$database",$conn);
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:
mysql_query("SET NAMES 'GBK'");
Then the page should also become:
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
OthersASP programs or other programming language websites can check the conversion encoding method on Baidu according to the actual situation.
The third type is that the browser causes garbled characters.
This may be caused by your webpage 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.
There are so many reasons and solutions for garbled HTML web pages. Friends who need it can save it. Please also continue to pay attention to other updates of this site.
Related reading:
The above is the detailed content of Why is html garbled and how to solve it. For more information, please follow other related articles on the PHP Chinese website!