PHP garbled problem, UTF-8 garbled problem summary

Source: Internet
Author: User

I. HTML page to UTF-8 encoding problem
1. Add a line before the title after the head:

<Meta http-equiv = 'content-type' Content = 'text/html; charset = UTF-8 '/>
The order must be in

The title may be garbled!

2.html file encoding:

Click the Editor menu: "file"-> "Save as", you can see the current file encoding, make sure the file encoding: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
3. HTML file header BOM:
When converting a file from another encoding to a UTF-8 encoding, sometimes a BOM label is added at the beginning of the file,
A bom label may cause garbled characters in the browser when displaying Chinese characters.
To delete this BOM Tag:
1. You can use Dreamweaver to open the file and save it again, removing the BOM tag!
2. You can open the file with EditPlus and choose "Preferences"> "Files"> "UTF-8 identifier" from the menu to "always remove signature ",
Save the file to remove the BOM label!
4. WEB Server UTF-8 coding problems:
If you follow the steps listed above, there are still Chinese garbled characters,
Check the encoding of your WEB server.
If you are using Apache, set charset in the configuration file to UTF-8 (only the method is listed here. For the specific format, refer to the apache configuration file)
If you are using Nginx, set charset in nginx. conf to UTF-8,
Find "charset gb2312;" or a similar statement and change it to "charset UTF-8 ;".
2. 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 Problems

Click the Editor menu: "file"-> "Save as", you can see the current file encoding, make sure the file encoding: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
3. php file header BOM problems:
The PHP file cannot have BOM labels.
Otherwise, the session cannot be used, and a similar prompt is displayed:
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 be output, but because the former PHP page has a BOM tag,
PHP regards this BOM label as output, so an error occurs!
Therefore, you must delete the BOM label on the PHP page.
To delete this BOM Tag:
1. You can use Dreamweaver to open the file and save it again, removing the BOM tag!
2. You can open the file with EditPlus and choose "Preferences"> "Files"> "UTF-8 identifier" from the menu to "always remove signature ",
Save the file to remove the BOM label!
4. UTF-8 coding problems when PHP saves files in the form of attachments:
PHP saves the file as an attachment. The file name must be GB2312 encoded,
Otherwise, if the file name contains Chinese characters, garbled characters will be displayed:
If your PHP is a UTF-8-encoded file,
You need to convert the file name variable from UTF-8 to GB2312:
Iconv ("UTF-8", "GB2312", "$ filename ");

5. Garbled text or "?" appears when the title of the document is truncated. Question mark:
When the title of an article is very long, a part of the title will be displayed and the title will be truncated,
Since a Chinese character in UTF-8 encoding format occupies 3 Characters in width,
When you intercept a title, it is sometimes only one or two characters in width of a Chinese character,
If the screenshot is not complete, garbled characters or "?" may occur. Question mark,
You can use the following function to extract the title:
Copy codeThe Code is 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;
}

Iii. Problem of using UTF-8 encoding in MYSQL database

1. Use phpmyadmin to create databases and data tables
When creating a database, set "sorting" 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 this field is used to store Chinese characters, set "sorting" to "utf8_general_ci ",

This field can be used by default if it is an English or number.

The corresponding SQL statement, for example:
Copy codeThe Code is as follows:
Create table 'test '(
'Id' int not null,
'Name' VARCHAR (10) character set utf8 COLLATE utf8_general_ci not null,
Primary key ('id ')
) ENGINE = MYISAM;

2. Use PHP to read and write Databases

After connecting to the database:

[Hide] $ connection = mysql_connect ($ host_name, $ host_user, $ host_pass );

Add two rows:
Copy codeThe Code is as follows:
Mysql_query ("set character set 'utf8'"); // read the database
Mysql_query ("set names 'utf8'"); // write Database

You can read and write the MYSQL database normally.

4. JS-related UTF-8 coding problems
1. Chinese garbled characters for reading cookies in JS

When writing cookies in PHP, You need to encode the Chinese characters in escape,
Otherwise, Chinese characters in the cookie read by JS will be garbled.
However, php itself does not have the escape function. We will write a new escape function:
Copy codeThe Code is 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 cookies, it uses unescape to decode them,

Then, the problem of Chinese garbled characters in cookies is solved.

2. External JS file UTF-8 coding problem

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

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

External JS files also need to be converted into UTF-8 files,

Otherwise, the system prompts that none of them are unsuccessful and no response is made when the function is called.

Click the Editor menu: "file"-> "Save as", you can see the current file encoding, make sure the file encoding: UTF-8,

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

5. UTF-8 coding problems related to FLASH

FLASH internally treats all strings with UTF-8 by default
1. FLASH reader (txt, html)
To encode a text file into a UTF-8
Click the Editor menu: "file"-> "Save as", you can see the current file encoding, make sure the file encoding: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
2. Read XML files in FLASH
To encode an XML file into a UTF-8
Click the Editor menu: "file"-> "Save as", you can see the current file encoding, make sure the file encoding: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
Write in XML 1st lines:

3. data returned by FLASH reading PHP
If PHP encoding itself is UTF-8, just echo it.
If PHP encoding itself is GB2312, you can convert PHP into UTF-8 encoding format file, direct echo can be
If PHP encoding is GB2312 and the file encoding format cannot be changed,
Use the following statement to convert the string to the encoding format of the UTF-8
$ New_str = iconv ("GB2312", "UTF-8", "$ str ");
Echo again.
4. read data from the FLASH database (MYSQL)
FLASH needs to read data from the database through PHP
PHP encoding is not important. The key is that if the database encoding is GB2312,
You need to use the following statement to convert the string to the encoding format of the UTF-8
$ New_str = iconv ("GB2312", "UTF-8", "$ str ");

5. FLASH writes data through PHP
In a word, the FLASH string is in UTF-8 format,
To convert to the corresponding encoding format, perform operations (write files, write databases, direct display, etc)
Still use iconv Function Conversion
6. FLASH uses local encoding (not recommended theoretically)
If you want FLASH to use local encoding instead of UTF-8 encoding
For Mainland China, the local code is GB2312 or GBK.
In the AS program, you can add the following code:
System. useCodepage = true;
All the characters in FLASH are encoded in GB2312.
All data imported to or exported from FLASH must be encoded and converted accordingly.
Because local encoding is used, garbled characters are generated for users in Traditional Chinese, so it is not recommended to use

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.