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);
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 'GBK'");
, then the page should also become:
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
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" />
or
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
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:
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!