Summary of UTF-8 coding problems encountered in web development page 1 of 2

Time:2019-11-28

There are mainly five aspects:

I. UTF-8 encoding of HTML pages

2. UTF-8 encoding of PHP page

The problem of using UTF-8 encoding in MySQL database

IV. JS related UTF-8 coding problems

V. UTF-8 encoding related to flash

I. UTF-8 encoding of HTML pages

1. After < head > and before < title >, add a line:

<meta http-equiv=’Content-Type’ content=’text/html; charset=utf-8‘ />

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

2. HTML file coding:

Click the menu of the editor: “file” – > “save as”, you can see the code of the current file, and make sure that the file code is: UTF-8. If it is ANSI, you need to change the code to: UTF-8.

3. HTML header BOM problem:

When a file is converted from other codes to UTF-8 codes, sometimes a BOM label is added at the beginning of the file, which may cause the browser to display garbled code in Chinese.

To delete this BOM label:

1. You can use Dreamweaver to open the file and save 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 identification”, it is set to “always delete signature”, and then save the file to remove BOM label!

4. UTF-8 encoding problem of web server:

If you follow the steps listed above and still have the problem of Chinese scrambling, please check the coding problem of the web server you are using

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

If you are using nginx, please set charset to UTF-8 in nginx.conf, find “charset GB2312;” or similar statement, and change it to: “charset UTF-8;”.

2. UTF-8 encoding of PHP page

1. Add a line at the beginning of the code:

header(“Content-Type: text/html;charset=utf-8”);

2. PHP file encoding

Click the menu of the editor: “file” – > “save as”, you can see the code of the current file, and make sure that the file code is: UTF-8. If it is ANSI, you need to change the code to: UTF-8.

3. PHP header BOM problem:

The PHP file must not have a BOM label. Otherwise, the session cannot be used, and a similar prompt appears:

Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent

This is because the whole page cannot have output when executing session_start(), but when there is a BOM tag in the previous PHP page, PHP takes this BOM tag as output, so there is an error!

So the PHP page must delete the BOM label

To delete this BOM label:

1. You can use Dreamweaver to open the file and save 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 identification”, it is set to “always delete signature”, and then save the file to remove BOM label!

4. When PHP saves the file as an attachment, UTF-8 encoding problem:

PHP saves the file as an attachment. The file name must be GB2312 code. Otherwise, if there is Chinese in the file name, it will display garbled Code:

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

iconv(“UTF-8”, “GB2312”, “$filename”);

5. When the title of the displayed article is truncated, there is a garbled or “?” Question mark:

Generally, when the title of an article is very long, a part of the title will be displayed, and the title of the article will be truncated. Because a Chinese character in UTF-8 encoding format will occupy three character width, when the title is truncated, sometimes only one or two character width of a Chinese character will be truncated. If the title is not completely truncated, there will be garbled code or “?” In the case of question mark, if you use the following function to intercept the title, 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;
}

12 read the full text on the next page