PHP unified page encoding to avoid garbled code, php page encoding garbled _ PHP Tutorial

Source: Internet
Author: User
PHP unified page code to avoid garbled code, php page code garbled. PHP unified page encoding to avoid garbled code. php page encoding and page encoding must be consistent with MySQL database encoding, html page encoding, and PHP or html file encoding. 1. MySQ 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:
The code is as follows:

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.
The 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:
The code is as follows:
Header ('content-type: text/html; charset = gbk ');

Php page code

1. set encoding in the file header
The 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 '); Difference
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.

Uniform page encoding: MySQL database encoding, html page encoding, PHP or html file encoding must all be consistent. 1. MySQ...

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.