If a website needs to be internationalized, it needs to convert the encoding from GB2312 to UTF-8, there are many of them You need to pay attention to the problem. If the conversion is not complete, there will be many encoding problems!
Converting 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");
2. PHP file encoding problem. Click the menu of the editor: "File" -> "Save As", you can 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.
3. PHP file header BOM problem: PHP files must not have BOM tags, otherwise, the session will not be usable, and there will be similar prompts: 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 have output, but when the BOM tag exists in the previous PHP page, PHP treats the BOM tag as output, so an error occurs! Therefore, the BOM tag must be deleted from the PHP page
How to delete this BOM tag:
1. You can open the file with Dreamweaver and resave it to remove the BOM tag!
2. You can open the file with EditPlus, and in the menu "Preferences"->"File"->"UTF-8 Identity", set it to: "Always delete signature", and then save the file. Remove the BOM tag!
3. UTF-8 encoding problem when PHP saves files as attachments: When PHP saves files as attachments, the file name must be GB2312 encoded, otherwise, if there is Chinese in the file name, it will be displayed Garbled characters: If your PHP itself is a file in UTF-8 encoding format, you need to convert the file name variable from UTF-8 to GB2312: iconv("UTF-8", "GB2312", "$filename");
4. When truncating and displaying the article title, garbled characters or "?" question marks appear:
Generally, when the title of an article is very long, part of the title will be displayed and the title of the article will be truncated. Since a Chinese character in UTF-8 encoding format will occupy 3 characters in width, when the title is intercepted, sometimes only the If the 1 character or 2 character width of a Chinese character is not completely intercepted, garbled characters or "?" question marks will appear. Use the following function to intercept the title and there will be no problem:
function get_brief_str($str, $max_length) { echo strlen($str) . ""; 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; }
The problem of using UTF-8 encoding in MYSQL database
1. Use phpmyadmin to create the database and data table. When creating the database, please set "Organization" to: "utf8_general_ci" or execute the statement:
CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
When creating a data table: If the field stores Chinese, you need to set "Organization" to: "utf8_general_ci". If the field stores English or numbers, the default is fine.
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:
$connection = mysql_connect($host_name, $host_user, $host_pass);
Add two lines:
mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库
I hope this article will be helpful to everyone’s PHP MySQL programming.