PHP garbled problem, UTF-8 garbled FAQ Summary _php Tips

Source: Internet
Author: User
Tags ord create database truncated
I. HTML page UTF-8 encoding problem
1. After head, add a line before title:

<meta http-equiv= ' content-type ' content= ' text/html; Charset=utf-8 '/>
The order can not be wrong, be sure to

The title displayed may be garbled!

2.html file Encoding problem:

Click on the Editor's menu: "File"-> "Save As", you can see the current file encoding to ensure that the file is encoded as: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
3.HTML file Header BOM problem:
When you convert a file from another encoding to a UTF-8 encoding, you sometimes add a BOM label at the beginning of the file.
A BOM label may cause the browser to appear garbled when it displays Chinese.
How to delete this BOM label:
1. You can use Dreamweaver to open the file and save it again, that is, you can remove the BOM label!
2. You can open the file with EditPlus, and in the menu "preferences"-> "file"-> "UTF-8 identity", set to: "Always delete signature",
Then save the file, that is, you can remove the BOM label!
4.WEB Server UTF-8 encoding problem:
If you follow the steps listed above, there are still Chinese garbled problems,
Please check your Web server's coding problem
If you are using Apache, please set the configuration file: CharSet: Utf-8 (here only the method, the specific format please refer to Apache configuration file)
If you are using Nginx, please set the nginx.conf: CharSet into Utf-8,
Specifically find "CharSet gb2312;" Or a similar statement, changed to: "CharSet utf-8;"
two. PHP page UTF-8 encoding problem
1. Add a line to the beginning of the code:
Header ("Content-type:text/html;charset=utf-8");

2.PHP file Encoding Problem

Click on the Editor's menu: "File"-> "Save As", you can see the current file encoding to ensure that the file is encoded as: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
3.PHP file Header BOM problem:
The PHP file must not have a BOM label
Otherwise, the session cannot be used, and there is a similar hint:
Warning:session_start () [Function.session-start]: Cannot send session cache Limiter-headers already sent
This is because when the session_start () is executed, the entire page cannot have output, but when a BOM tag exists on the previous PHP page,
PHP is the BOM label as output, so it went wrong!
So the PHP page must delete the BOM label
How to delete this BOM label:
1. You can use Dreamweaver to open the file and save it again, that is, you can remove the BOM label!
2. You can open the file with EditPlus, and in the menu "preferences"-> "file"-> "UTF-8 identity", set to: "Always delete signature",
Then save the file, that is, you can remove the BOM label!
4.PHP when saving files as an attachment, the UTF-8 encoding problem:
PHP saves the file as an attachment, and the filename must be GB2312 encoded,
Otherwise, if there is Chinese in the filename, it will be displayed garbled:
If your PHP itself is a file in UTF-8 encoded format,
You need to convert the filename variable from UTF-8 to GB2312:
Iconv ("UTF-8", "GB2312", "$filename");

5. When the title of the article is truncated, there is garbled or "? "Question mark:
General article title is very long time, will show a part of the title, will be truncated to the title of the article,
Because a Chinese character in a UTF-8 encoded format takes up 3 characters in width,
When you intercept a caption, you will sometimes only be able to intercept 1 characters or 2 characters in a Chinese character.
did not intercept the complete, will appear garbled or "? "The question Mark's case,
Using the following function to intercept the caption, there is no problem:
Copy Code code as follows:

function Get_brief_str ($str, $max _length)
{
echo strlen ($STR). " <br> ";
if (strlen ($STR) > $max _length)
{
$check _num = 0;
for ($i =0; $i < $max _length; $i + +)
{
if (Ord ($str [$i]) > 128)
$check _num++;
}

if ($check _num% 3 = 0)
$str = substr ($str, 0, $max _length). " ...";
else if ($check _num% 3 = 1)
$str = substr ($str, 0, $max _length + 2). " ...";
else if ($check _num% 3 = 2)
$str = substr ($str, 0, $max _length + 1). " ...";
}
return $str;
}

three. The problem of using UTF-8 encoding in MySQL database

1. Create databases and datasheets with phpMyAdmin
When you create a database, set the collation to: "Utf8_general_ci"
Or execute the statement:

CREATE DATABASE ' dbname ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;
When you create a datasheet: If the field is in Chinese, you need to set the collation to: "Utf8_general_ci",

If the field is stored in English or a number, the default is OK.

The corresponding SQL statement, for example:
Copy Code code as follows:

CREATE TABLE ' Test ' (
' ID ' INT not NULL,
' Name ' VARCHAR CHARACTER SET UTF8 COLLATE utf8_general_ci not NULL,
PRIMARY KEY (' id ')
) ENGINE = MYISAM;

2. Read and write database in PHP

After connecting to the database:

[hide] $connection = mysql_connect ($host _name, $host _user, $host _pass);

Add two lines:
Copy Code code as follows:

mysql_query ("Set character set ' UTF8 '");/Read Library
mysql_query ("Set names ' UTF8 ')"//write Library

You can read and write the MySQL database normally.

four. JS-related UTF-8 coding problem
1.JS read cookies in Chinese garbled problem

PHP to write a cookie when the Chinese characters to escape code,
Otherwise JS read to the cookie in the Chinese characters will be garbled.
But PHP itself has no escape function, and we're writing a new escape function:
Copy Code code as follows:

function Escape ($STR)
{
Preg_match_all ("/[\x80-\xff].| [\x01-\x7f]+/", $str, $r);
$ar = $r [0];
foreach ($ar as $k => $v)
{
if (Ord ($v [0]) < 128)
$ar [$k] = Rawurlencode ($v);
Else
$ar [$k] = "%u". Bin2Hex (Iconv ("UTF-8", "UCS-2", $v));
}
return join ("", $ar);
}

When JS reads a cookie, it is decoded with unescape,

Then solve the cookie in the Chinese garbled problem.

2. External JS file UTF-8 coding problem

When an HTML page or PHP page contains an external JS file,

If the HTML page or PHP page is a UTF-8 encoded format file,

The external JS file will also be converted into UTF-8 files,

Otherwise, it will appear, without the failure, to invoke the function without reacting.

Click on the Editor's menu: "File"-> "Save As", you can see the current file encoding to ensure that the file is encoded as: UTF-8,

If it is ANSI, you need to change the encoding to: UTF-8.

five. Flash-related UTF-8 coding problem

Flash internal to all strings, by default are treated as UTF-8
1.FLASH read the general text of this document (txt,html)
To save the encoding of a text file as a UTF-8
Click on the Editor's menu: "File"-> "Save As", you can see the current file encoding to ensure that the file is encoded as: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
2.FLASH Read XML file
To save the encoding of an XML file as a UTF-8
Click on the Editor's menu: "File"-> "Save As", you can see the current file encoding to ensure that the file is encoded as: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
In the 1th line of XML, write:

3.FLASH Read PHP return data
If the PHP encoding itself is UTF-8, the direct echo is OK.
If the PHP encoding itself is GB2312, you can dump PHP into a UTF-8 encoded file, and direct echo is OK.
If the PHP encoding itself is GB2312 and does not allow you to change the encoding format of the file,
Use the following statement to convert a string to a UTF-8 encoding format
$new _str = Iconv ("GB2312", "UTF-8", "$str");
Echo will be all right.
4.FLASH Read Database (MYSQL) data
Flash to read data in the database through PHP
The coding of PHP itself is not important, the key is if the encoding of the database is GB2312,
Use the following statement to convert a string to a UTF-8 encoding format
$new _str = Iconv ("GB2312", "UTF-8", "$str");

5.FLASH writes data through PHP
In a word, the string transmitted by Flash is UTF-8 format,
To convert to the appropriate encoding format, and then manipulate (write file, write database, direct display, etc.)
or use the Iconv function to convert
6.FLASH use local encoding (theoretically not recommended)
If you want Flash to not use UTF-8 encoding, instead use a local encoding
For mainland China, the local code is GB2312 or GBK
In the AS program, you can add the following code:
System.usecodepage = true;
So all the characters in Flash are encoded using GB2312.
All data imported into Flash or exported from Flash should be encoded accordingly.
Because the use of local encoding, will result in the use of traditional Chinese areas of the user generated garbled, so do not recommend the use of
Related Article

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.