PHP unified page encoding to avoid Garbled text, php page encoding Garbled text

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.