PHP Chinese garbled solution

【Foreword】

   Generally speaking, there are two reasons for the appearance of garbled characters. One is due to the wrong encoding (charset) setting, which causes the browser to parse with the wrong encoding, resulting in a messy screen full of "books from heaven". Open with wrong encoding, and then save, for example, a text file originally encoded in GB2312, but opened and saved in UTF-8 encoding, there will be a problem of garbled characters.

 

【main body】

The first: method to solve the problem of Chinese garbled characters in HTML

If your HTML file is garbled, then you can add UTF8 encoding (international encoding) to the head tag: UTF-8 is a country-free encoding, that is, independent of any language, any language can be used of.

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

Example

Our current HTML5 file, setting the encoding is simpler, like the following

Second, mixed HTML and PHP page solution

How to mix HTML and PHP, in addition to the operation according to the first method, you also need to add this code to the top of the PHP file:

<?php header("content-type:text/html;charset=utf-8"); //Set encoding?>  

The third, the Chinese garbled problem of pure PHP pages (data is static)

If your PHP page is garbled, just add the following code at the beginning of the page.

<?php header("content-type:text/html;charset=utf-8"); //Set encoding?>

Fourth, PHP+Mysql Chinese garbled problem

In addition to the third operation, you also need to add database coding before your data query/modification/addition. Moreover, it is worth noting that the UTF8 here is different from the previous one, and there is no horizontal line in the middle.

<?php mysql_query('SET NAMES UTF8'); //接下来的就是查出数据或者修改,增加?> 

如何你使用的 MySQL 版本在 4.1 或更高版本,可以在链接数据库操作后,设置一个字符编码,像下面这样

UTF-8 编码只是其中一种编码,如果不想使用 utf-8 编码,也可以使用其他编码,只需将 UTF-8 换成你想使用的编码就可以,目前中文网站开发中主要用的是 GB2312 和 UTF-8 两种编码。

有一点要注意的是在需要做数据库操作的php程序前加的 mysql_query (" set names '编码'");编码,一定要和 php 编码一致,如果 php 编码是 gb2312 那 mysql 编码就是 gb2312 ,如果是 utf-8 那 mysql 编码就是 utf8 ,这样插入或检索数据时就不会出现乱码了

 

 

 

 

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326229731&siteId=291194637