Summary of UTF-8 coding problems encountered in Web Development page 1/2

Source: Internet
Author: User

There are five main aspects:

I. HTML page to UTF-8 encoding problem

2. PHP page to UTF-8 encoding problem

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

4. JS-related UTF-8 coding problems

5. UTF-8 coding problems related to FLASH

I. HTML page to UTF-8 encoding problem

1. After

<Meta http-equiv = 'content-type' Content = 'text/html; charset = UTF-8 '/>

The order cannot be wrong. Be sure to add it before the <title> tag. Otherwise, if there are Chinese characters between <title> and </title>, the title may be garbled!

2.html file encoding:

Click the Editor menu: file-> Save As, you can see the encoding of the current file, make sure 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:

When converting a file from another encoding to a UTF-8 encoding, sometimes a BOM label is added at the beginning of the file, and 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 in the menu "Preferences"-> "Files"-> "UTF-8 identifier", set to "always Delete Signature", and then save the file, you can remove the BOM label!

4. WEB Server UTF-8 coding problems:

If you have followed the steps listed above and still encountered 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, see the apache configuration file ).

If you are using Nginx, set charset in nginx. conf to UTF-8. For more information, see "charset gb2312;" or 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 encoding of the current file, make sure 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 problems:

The PHP file cannot have the BOM label. 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. However, because the former PHP page has a BOM label, PHP regards this BOM label as output, so an error occurred!

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 in the menu "Preferences"-> "Files"-> "UTF-8 identifier", set to "always Delete Signature", and then save the file, you can 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, it will be garbled:

If your PHP itself is a UTF-8-encoded file, you need to convert the file name variable from the UTF-8 to GB2312:

Iconv ("UTF-8", "GB2312", "$ filename ");

5. Garbled text or "?" appears when the title of the document is truncated. Question mark:

General article title is very long, will display part of the title, will be truncated, because a UTF-8 encoding format of Chinese characters will occupy 3 Characters in width, when intercepting the title, sometimes only one or two characters in width of one Chinese character are captured. If the width is not completely captured, garbled characters or "?" If you use the following function to intercept the title of a question mark, there will be no problem:

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;
}

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.