PHP page to UTF-8 Encoding Problems
1. Add a line at the beginning of the Code:
Copy codeThe Code is as follows:
Header ("Content-Type: text/html; charset = UTF-8 ");
2. php file Encoding Problems
Click the Editor menu: "file"-> "Save as", you can see the current file encoding, make sure the file encoding: 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 BOM labels.
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, but because the former PHP page has a BOM tag,
PHP regards this BOM label as output, so an error occurs!
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 choose "Preferences"> "Files"> "UTF-8 identifier" from the menu to "always remove signature ",
Save the file to 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, garbled characters will be displayed:
If your PHP is a UTF-8-encoded file,
You need to convert the file name variable from UTF-8 to GB2312:
Iconv ("UTF-8", "GB2312", "$ filename ");
How to Use programs to intercept instance characters
Copy codeThe Code is as follows:
Function utf8_substr ($ str, $ len)
{
For ($ I = 0; $ I <$ len; $ I ++)
{
$ Temp_str = substr ($ str, 0, 1 );
If (ord ($ temp_str) & gt; 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 );
}
Issues with using UTF-8 encoding for MYSQL Databases
1. Use phpmyadmin to create databases and data tables
When creating a database, set "sorting" to "utf8_general_ci"
Or execute the statement:
Copy codeThe Code is as follows:
Create database 'dbname' default character set utf8 COLLATE utf8_general_ci;
When creating a data table: If this field is used to store Chinese characters, set "sorting" to "utf8_general_ci ",
This field can be used by default if it is an English or number.
The corresponding SQL statement, for example:
Copy 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:
Copy codeThe Code is as follows:
$ Connection = mysql_connect ($ host_name, $ host_user, $ host_pass );
Add two rows:
Copy codeThe Code is as follows:
Mysql_query ("set character set 'utf8'"); // read the database
Mysql_query ("set names 'utf8'"); // write Database
You can read and write the MYSQL database normally.
The environment made of appserv-win32-2.5.10, when installing this package with the default utf8 encoding.
When writing a database connection file, write it:
Copy codeThe Code is as follows:
$ Conn = mysql_connect ("$ host", "$ user", "$ password ");
Mysql_query ("set names 'utf8 '");
Mysql_select_db ("$ database", $ conn );
Then pay attention to this sentence when you make the page:
Copy codeThe Code is as follows:
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
In this way, no matter whether the database is entered in Chinese or displayed on the page, it will be normal.
In DW CS4, The utf8 page is generated by default.
Similarly, if it is written:
Copy codeThe Code is as follows:
Mysql_query ("set names 'gbk '");
The page should also be changed:
Copy codeThe Code is as follows:
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
In conclusion, we can solve the garbled problem easily by unifying the page encoding, especially when setting the set names in mysql_query (), it is necessary to make statistics on the page and database encoding.