PHP page UTF-8 encoding problem
1. Add a line to the beginning of the code:
Copy Code code as follows:
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 is encoded as: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
3.PHP file Header BOM problem:
The PHP file must not have a BOM label
Otherwise, the session cannot be used, and there is a similar hint:
Warning:session_start () [Function.session-start]: Cannot send session cache Limiter-headers already sent
This is because when the session_start () is executed, the entire page cannot have output, but when a BOM tag exists on the previous PHP page,
PHP is the 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 open the file with EditPlus, and in the menu "preferences"-> "file"-> "UTF-8 identity", set to: "Always delete signature",
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 saves the file as an attachment, and the filename must be GB2312 encoded,
Otherwise, if there is Chinese in the filename, it will be displayed garbled:
If your PHP itself is a file in UTF-8 encoded format,
You need to convert the filename variable from UTF-8 to GB2312:
Iconv ("UTF-8", "GB2312", "$filename");
Using program to instance character interception method
Copy Code code as follows:
function Utf8_substr ($STR, $len)
{
for ($i =0; $i < $len; $i + +)
{
$temp _str=substr ($str, 0, 1);
if (Ord ($temp _str) > 127) {
$i + +;
if ($i < $len) {
$new _str[]=substr ($str, 0, 3);
$str =substr ($STR, 3);
}
}else {
$new _str[]=substr ($str, 0, 1);
$str =substr ($STR, 1);
}
}
return join ($new _str);
}
The problem of using UTF-8 encoding in MySQL database
1. Create databases and datasheets with phpMyAdmin
When you create a database, set the collation to: "Utf8_general_ci"
Or execute the statement:
Copy Code code as follows:
CREATE DATABASE ' dbname ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;
When you create a datasheet: If the field is in Chinese, you need to set the collation to: "Utf8_general_ci",
If the field is stored in English or a number, the default is OK.
The corresponding SQL statement, for example:
Copy Code code as follows:
CREATE TABLE ' Test ' (
' ID ' INT not NULL,
' Name ' VARCHAR CHARACTER SET UTF8 COLLATE utf8_general_ci not NULL,
PRIMARY KEY (' id ')
) ENGINE = MYISAM;
2. Read and write database in PHP
After connecting to the database:
Copy Code code as follows:
$connection = mysql_connect ($host _name, $host _user, $host _pass);
Add two lines:
Copy Code code as follows:
mysql_query ("Set character set ' UTF8 '");/Read Library
mysql_query ("Set names ' UTF8 ')"//write Library
You can read and write the MySQL database normally.
Use the appserv-win32-2.5.10 to do the environment, when loading this package with the default UTF8 encoding.
When writing a database connection file, write:
Copy Code code as follows:
$conn = mysql_connect ("$host", "$user", "$password");
mysql_query ("SET NAMES ' UTF8 '");
mysql_select_db ("$database", $conn);
And then when you do the page, pay attention to this sentence:
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
So no matter the Chinese of the input database, or the page display, it is normal.
In the DW CS4 Edition, the default generation is also the UTF8 page.
Similarly, if you start by writing a database connection file:
Copy Code code as follows:
mysql_query ("SET NAMES ' GBK '");
The page should also be changed accordingly:
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
Summary, finally, the main page coding to be unified can be very convenient to solve the garbled problem, especially in mysql_query () the Set names settings must and the page and the database code statistics one can.