1. The issue of HTML page to UTF-8 encoding
1. After the head, add a line before the title:
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
The order cannot be wrong, it must be
The title displayed may be garbled!
File encoding issues:
Click the editor menu: "File"->Save As" to see the encoding of the current file. Make sure the file encoding is: UTF-8.
If it is ANSI, you need to change the encoding to: UTF-8.
File header BOM problem:
When converting a file from other encodings to UTF-8 encoding, sometimes a BOM tag will be added to the beginning of the file.
In a BOM tag, it may cause garbled codes when the browser displays Chinese.
How to delete this BOM tag:
1. You can open the file with Dreamweaver and save it again, that is, you can remove the BOM tag!
2. You can open the file with EditPlus and set it to: "Always delete signature" in the menu "Preferences" -> "File" -> "UTF-8 Identification".
Then save the file and you can remove the BOM tag!
Server UTF-8 encoding problem:
If you follow the steps listed above, you still have problems with Chinese garbled code.
Please check the encoding 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. For the specific format, please refer to the configuration file of apache)
If you are using Nginx, please set the charset to utf-8.
Find "charset gb2312;" or 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");
File encoding issues
Click the editor menu: "File"->Save As" to see the encoding of the current file. Make sure the file encoding is: UTF-8.
If it is ANSI, you need to change the encoding to: UTF-8.
File header BOM problem:
PHP files must not have BOM tags
Otherwise, the session will not be used, and there will be similar prompts:
Warning: session_start() [-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 tag exists in the previous PHP page,
PHP treats this BOM tag as output, so an error occurs!
Therefore, the PHP page must delete the BOM tag
How to delete this BOM tag:
1. You can open the file with Dreamweaver and save it again, that is, you can remove the BOM tag!
2. You can open the file with EditPlus and set it to: "Always delete signature" in the menu "Preferences" -> "File" -> "UTF-8 Identification".
Then save the file and you can remove the BOM tag!
When saving files in attachments, UTF-8 encoding problems:
PHP saves files in attachment form, the file name must be GB2312 encoding.
Otherwise, if there is Chinese in the file name, it will display garbled code:
If your PHP itself 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. When truncating the title of the article, a garbled or "?" question mark appears:
Generally, when the title of an article is very long, some titles will be displayed and the title of the article will be cut off.
Since a Chinese character in UTF-8 encoding format will occupy 3 characters widths,
When intercepting the title, sometimes only one character or two character width of one Chinese character will be intercepted.
If the complete intercept is not intercepted, there will be a garbled code or a "?" 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;
}
3. Problem of using UTF-8 encoding in MYSQL database
1. Create database and data tables with phpmyadmin
When creating a database, please set "Collect" to: "utf8_general_ci"
Or execute statement:
CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
When creating a data table: If this field is stored in Chinese, you need to set "Collect" to: "utf8_general_ci".
If this field stores English or numbers, the default is OK.
The corresponding SQL statement, for example:
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 lines:
mysql_query("set character set 'utf8'");//Read library
mysql_query("set names 'utf8'");//Write library
You can read and write MYSQL database normally.
4. JS-related UTF-8 encoding issues
Chinese garbled problem reading cookies
When writing cookies, Chinese characters need to be encapsulated.
Otherwise, the Chinese characters in the cookie will be garbled.
But php itself does not have an escape function, so 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);
}
When JS reads cookies, use unescape to decode them.
Then it solves the problem of Chinese garbled code in cookies.
2. UTF-8 encoding problem of external JS file
When an HTML page or PHP page contains an external JS file,
If the HTML page or PHP page is a UTF-8 encoded file,
External JS files should also be converted into UTF-8 files.
Otherwise, there will be no case where the inclusion is unsuccessful and there is no response when calling the function.
Click the editor menu: "File"->Save As" to see the encoding of the current file. Make sure the file encoding is: UTF-8.
If it is ANSI, you need to change the encoding to: UTF-8.
5. UTF-8 encoding issues related to FLASH
All strings inside FLASH are processed in UTF-8 by default.
Read the text normal file (txt, html)
To save the encoding of the text file as UTF-8
Click the editor menu: "File"->Save As" to see the encoding of the current file. Make sure the file encoding is: UTF-8.
If it is ANSI, you need to change the encoding to: UTF-8.
Read XML files
To save the encoding of the XML file as UTF-8
Click the editor menu: "File"->Save As" to see the encoding of the current file. Make sure the file encoding is: UTF-8.
If it is ANSI, you need to change the encoding to: UTF-8.
Write on line 1 of XML:
Read PHP to return data
If the PHP encoding itself is UTF-8, just echo it
If the PHP encoding itself is GB2312, you can convert PHP to a UTF-8 encoding file, just echo it
If the PHP encoding itself is GB2312, and the encoding format of the file is not allowed,
Use the following statement to convert the string into UTF-8 encoding format
$new_str = iconv("GB2312", "UTF-8", "$str");
Just echo again
Read Data from a Database (MYSQL)
FLASH needs to read data in the database through PHP
The encoding of PHP itself is not important, the key is if the encoding of the database is GB2312.
You need to use the following statement to convert the string into UTF-8 encoding format
$new_str = iconv("GB2312", "UTF-8", "$str");
Write data through PHP
In a word, the string passed by FLASH is in UTF-8 format.
To convert it to the corresponding encoding format, then operate (write files, write databases, display directly, etc.)
Or use iconv function to convert
Use local encoding (in theory not recommended)
If you want FLASH to not use UTF-8 encoding, you use local encoding instead
For mainland China, the local encoding is GB2312 or GBK
In the AS program, you can add the following code:
= true;
Then all characters in FLASH are encoded using GB2312
All data imported into or exported from FLASH should be converted accordingly
Because using local encoding will cause garbled code for users in traditional Chinese regions, so it is not recommended to use
1. After the head, add a line before the title:
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
The order cannot be wrong, it must be
The title displayed may be garbled!
File encoding issues:
Click the editor menu: "File"->Save As" to see the encoding of the current file. Make sure the file encoding is: UTF-8.
If it is ANSI, you need to change the encoding to: UTF-8.
File header BOM problem:
When converting a file from other encodings to UTF-8 encoding, sometimes a BOM tag will be added to the beginning of the file.
In a BOM tag, it may cause garbled codes when the browser displays Chinese.
How to delete this BOM tag:
1. You can open the file with Dreamweaver and save it again, that is, you can remove the BOM tag!
2. You can open the file with EditPlus and set it to: "Always delete signature" in the menu "Preferences" -> "File" -> "UTF-8 Identification".
Then save the file and you can remove the BOM tag!
Server UTF-8 encoding problem:
If you follow the steps listed above, you still have problems with Chinese garbled code.
Please check the encoding 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. For the specific format, please refer to the configuration file of apache)
If you are using Nginx, please set the charset to utf-8.
Find "charset gb2312;" or 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");
File encoding issues
Click the editor menu: "File"->Save As" to see the encoding of the current file. Make sure the file encoding is: UTF-8.
If it is ANSI, you need to change the encoding to: UTF-8.
File header BOM problem:
PHP files must not have BOM tags
Otherwise, the session will not be used, and there will be similar prompts:
Warning: session_start() [-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 tag exists in the previous PHP page,
PHP treats this BOM tag as output, so an error occurs!
Therefore, the PHP page must delete the BOM tag
How to delete this BOM tag:
1. You can open the file with Dreamweaver and save it again, that is, you can remove the BOM tag!
2. You can open the file with EditPlus and set it to: "Always delete signature" in the menu "Preferences" -> "File" -> "UTF-8 Identification".
Then save the file and you can remove the BOM tag!
When saving files in attachments, UTF-8 encoding problems:
PHP saves files in attachment form, the file name must be GB2312 encoding.
Otherwise, if there is Chinese in the file name, it will display garbled code:
If your PHP itself 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. When truncating the title of the article, a garbled or "?" question mark appears:
Generally, when the title of an article is very long, some titles will be displayed and the title of the article will be cut off.
Since a Chinese character in UTF-8 encoding format will occupy 3 characters widths,
When intercepting the title, sometimes only one character or two character width of one Chinese character will be intercepted.
If the complete intercept is not intercepted, there will be a garbled code or a "?" question mark.
If you use the following function to intercept the title, there will be no problem:
Copy the 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;
}
3. Problem of using UTF-8 encoding in MYSQL database
1. Create database and data tables with phpmyadmin
When creating a database, please set "Collect" to: "utf8_general_ci"
Or execute statement:
CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
When creating a data table: If this field is stored in Chinese, you need to set "Collect" to: "utf8_general_ci".
If this field stores English or numbers, the default is OK.
The corresponding SQL statement, for example:
Copy the 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 lines:
Copy the codeThe code is as follows:
mysql_query("set character set 'utf8'");//Read library
mysql_query("set names 'utf8'");//Write library
You can read and write MYSQL database normally.
4. JS-related UTF-8 encoding issues
Chinese garbled problem reading cookies
When writing cookies, Chinese characters need to be encapsulated.
Otherwise, the Chinese characters in the cookie will be garbled.
But php itself does not have an escape function, so we write a new escape function:
Copy the 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, use unescape to decode them.
Then it solves the problem of Chinese garbled code in cookies.
2. UTF-8 encoding problem of external JS file
When an HTML page or PHP page contains an external JS file,
If the HTML page or PHP page is a UTF-8 encoded file,
External JS files should also be converted into UTF-8 files.
Otherwise, there will be no case where the inclusion is unsuccessful and there is no response when calling the function.
Click the editor menu: "File"->Save As" to see the encoding of the current file. Make sure the file encoding is: UTF-8.
If it is ANSI, you need to change the encoding to: UTF-8.
5. UTF-8 encoding issues related to FLASH
All strings inside FLASH are processed in UTF-8 by default.
Read the text normal file (txt, html)
To save the encoding of the text file as UTF-8
Click the editor menu: "File"->Save As" to see the encoding of the current file. Make sure the file encoding is: UTF-8.
If it is ANSI, you need to change the encoding to: UTF-8.
Read XML files
To save the encoding of the XML file as UTF-8
Click the editor menu: "File"->Save As" to see the encoding of the current file. Make sure the file encoding is: UTF-8.
If it is ANSI, you need to change the encoding to: UTF-8.
Write on line 1 of XML:
Read PHP to return data
If the PHP encoding itself is UTF-8, just echo it
If the PHP encoding itself is GB2312, you can convert PHP to a UTF-8 encoding file, just echo it
If the PHP encoding itself is GB2312, and the encoding format of the file is not allowed,
Use the following statement to convert the string into UTF-8 encoding format
$new_str = iconv("GB2312", "UTF-8", "$str");
Just echo again
Read Data from a Database (MYSQL)
FLASH needs to read data in the database through PHP
The encoding of PHP itself is not important, the key is if the encoding of the database is GB2312.
You need to use the following statement to convert the string into UTF-8 encoding format
$new_str = iconv("GB2312", "UTF-8", "$str");
Write data through PHP
In a word, the string passed by FLASH is in UTF-8 format.
To convert it to the corresponding encoding format, then operate (write files, write databases, display directly, etc.)
Or use iconv function to convert
Use local encoding (in theory not recommended)
If you want FLASH to not use UTF-8 encoding, you use local encoding instead
For mainland China, the local encoding is GB2312 or GBK
In the AS program, you can add the following code:
= true;
Then all characters in FLASH are encoded using GB2312
All data imported into or exported from FLASH should be converted accordingly
Because using local encoding will cause garbled code for users in traditional Chinese regions, so it is not recommended to use