PHP garbled problem, UTF-8 (garbled) _php tutorial

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

The order must not be wrong, must be in
The caption 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 encoding: UTF-8,
In the case of 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 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 displaying Chinese.
How to delete this BOM label:
1. You can open the file with Dreamweaver 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, you can remove the BOM label!
4.WEB Server UTF-8 encoding problem:
If you follow the steps listed above, or there is a Chinese garbled problem,
Please check the encoding problem of your Web server you are using
If you are using Apache, please set the: CharSet in the configuration file: Utf-8 (only the method is listed here, please refer to Apache configuration file for details)
If you are using Nginx, please set the nginx.conf: CharSet to Utf-8,
Specifically find "CharSet gb2312;" Or a similar statement, changed to: "CharSet utf-8;".
Two. PHP page to UTF-8 encoding problem
1. Add a line at the beginning of the code:
Header ("Content-type:text/html;charset=utf-8");
2.PHP File Encoding Issues
Click on the Editor's menu: "File", "Save As", you can see the current file encoding, to ensure that the file encoding: UTF-8,
In the case of ANSI, you need to change the encoding to: UTF-8.
3.PHP file Header BOM problem:
PHP files must not have BOM labels
Otherwise, the session cannot be used, and there are similar hints:
Warning:session_start () [Function.session-start]: Cannot send session cache Limiter-headers already sent
This is because, when executing session_start (), the entire page cannot have output, but when the BOM label exists because of the previous PHP page,
PHP put this BOM label as output, so it went wrong!
So php page must delete BOM label
How to delete this BOM label:
1. You can open the file with Dreamweaver 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, you can remove the BOM label!
4.PHP UTF-8 encoding problem when saving files as attachments:
PHP saves the file as an attachment, and the filename must be GB2312 encoded.
Otherwise, if there is Chinese in the file name, it will be garbled:
If your PHP itself is a file in UTF-8 encoded format,
The file name variable needs to be converted from UTF-8 to GB2312:
Iconv ("UTF-8", "GB2312", "$filename");

5. When I truncate the title of the article, there is garbled or "? Question mark:
General article title very long time, will show a part of the title, will be truncated the title of the article,
Because the Chinese characters in a UTF-8 encoded format occupy 3 character widths,
When a caption is truncated, it is sometimes only 1 characters or 2 character widths that are truncated to one of the Chinese characters.
Not intercepted complete, will appear garbled or "? "The case of a question mark,
Using the following function to intercept the title, there is no problem:
function Get_brief_str ($str, $max _length)
{
echo strlen ($STR). "
";
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. mysql database using UTF-8 encoding problem

1. Create a database and data table 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 creating a data table: If the field is in Chinese, you need to set the collation to: "Utf8_general_ci",
If the field is in English or a number, it will be the default.
The corresponding SQL statement, for example:
CREATE TABLE ' Test ' (
' ID ' INT not NULL,
' Name ' VARCHAR (Ten) CHARACTER SET UTF8 COLLATE utf8_general_ci not NULL,
PRIMARY KEY (' id ')
) ENGINE = MYISAM;
2. Read and write databases in PHP
After connecting to the database:
[hide] $connection = mysql_connect ($host _name, $host _user, $host _pass);
Add two lines:
mysql_query ("Set character set ' UTF8 ');//Read Library
mysql_query ("Set names ' UTF8 '");//write Library
will be able to read and write the MySQL database properly.


Four. JS-related UTF-8 coding problems
The problem of Chinese garbled characters in 1.JS reading cookie

PHP will need to write the Chinese characters to escape code when writing cookies,
Otherwise, JS reads the Chinese characters in the cookie will be garbled.
But PHP itself does not have escape function, we write a new escape function:
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);
}
JS read the cookie, with unescape decoding,
Then we will solve the problem of Chinese garbled in the cookie.
2. External JS file UTF-8 encoding problem
When an HTML page or a PHP page contains an external JS file,
If the HTML page or PHP page is a file in UTF-8 encoded format,
The external JS files are also converted to UTF-8 files,
Otherwise, there is no case of unsuccessful, no response when calling the function.
Click on the Editor's menu: "File", "Save As", you can see the current file encoding, to ensure that the file encoding: UTF-8,
In the case of ANSI, you need to change the encoding to: UTF-8.
Five. Flash-related UTF-8 coding issues
Flash internal to all strings, the default is to UTF-8 processing
1.FLASH reading ordinary paper (txt,html)
To save the encoding of a text file as UTF-8
Click on the Editor's menu: "File", "Save As", you can see the current file encoding, to ensure that the file encoding: UTF-8,
In the case of ANSI, you need to change the encoding to: UTF-8.
2.FLASH Read XML file
To save the encoding of an XML file as UTF-8
Click on the Editor's menu: "File", "Save As", you can see the current file encoding, to ensure that the file encoding: UTF-8,
In the case of 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 code itself is UTF-8, the direct echo can be
If the PHP code itself is GB2312, you can dump PHP into UTF-8 encoded file, directly echo can
If the PHP encoding itself is GB2312, and it is not allowed to change the encoding format of the file,
Convert the string to a UTF-8 encoded format using the following statement
$new _str = Iconv ("GB2312", "UTF-8", "$str");
And then echo will be all right.
4.FLASH Read Database (MYSQL) data
Flash to read data from the database through PHP
The encoding of PHP itself is not important, the key is if the database encoding is GB2312,
You need to convert the string to a UTF-8 encoded format using the following statement
$new _str = Iconv ("GB2312", "UTF-8", "$str");

5.FLASH Write Data via PHP
In a word, the string sent by Flash is in UTF-8 format,
To convert to the appropriate encoding format, and then manipulate (write file, write database, direct display, etc.)
or using the Iconv function to convert
6.FLASH using local encoding (not recommended in theory)
If you want Flash to not use UTF-8 encoding, use local encoding
For mainland China, local codes are GB2312 or GBK
Within 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 and converted accordingly.
Because the use of local encoding will cause users in the traditional Chinese region to generate garbled, so it is not recommended to use


Excerpted from Melons

http://www.bkjia.com/PHPjc/478297.html www.bkjia.com true http://www.bkjia.com/PHPjc/478297.html techarticle I. HTML page to UTF-8 encoding problem 1. After head, add a line before title: Meta Http-equiv=content-type content=text/html; charset=utf-8/order cannot be wrong, Be sure to display the caption ...

  • 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.