PHP unified page encoding to avoid Garbled text, php page encoding Garbled text
Unified page Encoding
MySQL database encoding, html page encoding, PHP or html file encoding must all be consistent.
1. MySQL database encoding:
Specify the encoding (such as gbk_chinese_ci) when creating a database. do not specify the encoding when creating a data table, creating a field, or inserting data. The database encoding is automatically inherited.
The database is also encoded when it is connected. After connecting to the database, you can execute mysql_query ('set NAMES gbk'); // replace gbk with your encoding, such as utf8.
2. html page encoding refers to the setting of this line:
Copy codeThe Code is as follows:
<Meta http-equiv = "Content-Type" content = "text/html; charset = gbk"/>
3. PHP or html file encoding:
Use editplus to open the PHP file or html file. When saving the file separately, select the encoding. If the database and page encoding are gbk, select ansi as the encoding here. If the database and page encoding are UTF-8, then UTF-8 is also selected here.
4. The data transmitted in Javascript or Flash is UTF-8 encoded:
In addition, the data transmitted in Javascript or Flash is UTF-8 encoded. If the database and page encoding are gbk, transcoding is required and then written to the database.
Copy codeThe Code is as follows:
Iconv ('utf-8', 'gbk', $ content );
5. In the PHP program, you can add a line to specify the encoding of the PHP source program:
Copy codeThe Code is as follows:
Header ('content-type: text/html; charset = gbk ');
Php page code
1. Set encoding in the file header
Copy codeThe Code is as follows:
<? Php
@ Header ('content-type: text/html; charset = UTF-8 ');
?>
2. Differences between header and meta
Use @ header ('content-type: text/html; charset = gbk'); and <meta http-equiv = "Content-Type" Content = "text/html; charset = gbk "/>
They all tell the browser what encoding is used to display the webpage. What is the difference? The header sends the original HTTP header, and does not leave anything in the webpage. meta is written in the webpage.
First, if the webpage does not contain meta, sending the HTTP header will take effect.
Second, use the header () function to send the original HTTP header, which can contain more content. encoding is only one of them.
Third, in some cases, do not display any content on the webpage, but notify the browser of the encoding used for subsequent actions.