A summary of UTF-8 coding problems encountered in WEB development 1th/2 page _ Related tips

Source: Internet
Author: User
Tags strlen truncated

There are five main areas:

One.. HTML page UTF-8 encoding problem

Two. PHP page UTF-8 encoding problem

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

Four. JS-related UTF-8 coding problem

Five. Flash-related UTF-8 coding problem

I. HTML page UTF-8 encoding problem

1. Add a line before the

<meta http-equiv= ' content-type ' content= ' text/html; Charset=utf-8 '/>

The order can not be wrong, must be added before <title> tags, otherwise, if the <title> and </title> between the words, the display title 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 is: UTF-8, if it is ANSI, you need to change the code 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 tag at the beginning of the file, and 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 use EditPlus to open the file, and in the menu "preferences"-> "file"-> "UTF-8 identity", set to: "Always delete the signature", and 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, or if there is a Chinese garbled problem, please check your Web server's coding problem

If you are using Apache, please set the: CharSet in the configuration file: Utf-8 (here only the methods are listed, please refer to the Apache configuration file for specific format).

If you are using Nginx, please set the nginx.conf: CharSet to Utf-8, 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 encoding is: UTF-8, if it is ANSI, you need to change the code to: UTF-8.

3.PHP file Header BOM problem:

PHP file must not have a BOM label, otherwise, the session can not use the situation, and have similar tips:

Warning:session_start () [Function.session-start]: Cannot send session cache Limiter-headers already sent

This is because, in the execution of Session_Start (), the entire page can not have output, but when the previous PHP page exists BOM label, PHP to this 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 use EditPlus to open the file, and in the menu "preferences"-> "file"-> "UTF-8 identity", set to: "Always delete the signature", and 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 as an attachment to save the file, file name must be GB2312 code, otherwise, if the file name in Chinese, will be displayed garbled:

If your PHP itself is a UTF-8 encoded file, you need to turn 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 for a long time, will show a part of the title, will be truncated to the title of the article, because a UTF-8 encoded format of the Chinese characters will occupy 3 character width, the interception of the title, sometimes only intercepted to a Chinese character of 1 characters or 2 character width, did not intercept the complete, will appear garbled or "? Question mark, use the following function to intercept the title, there is 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;
}

Current 1/2 page 12 Next read the full text

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.