Home >Backend Development >PHP Tutorial >PHP garbled code problem, UTF-8 (garbled code)_PHP tutorial
1. HTML page conversion to UTF-8 encoding problem
1. Add a line after head and before title:
The order cannot be wrong, it must be in
The displayed title may be garbled!
2.html file encoding problem:
Click the editor's menu: "File" -> "Save As", you can see the encoding of the current file, make sure the file encoding is: UTF-8,
If it is ANSI, the encoding needs to be changed to: UTF-8.
3.HTML file header BOM problem:
When converting files from other encodings to UTF-8 encoding, a BOM tag is sometimes added to the beginning of the file,
The BOM tag may cause the browser to display garbled characters when displaying Chinese characters.
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 remove signature",
Then save the file and the BOM tag can be removed!
4.WEB server UTF-8 encoding problem:
If you have followed the steps listed above and still have garbled Chinese characters,
Please check the encoding problem of the WEB server you are using
If you are using Apache, please set the 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 in nginx.conf to utf-8,
Specifically find "charset gb2312;" or a similar statement and change it to: "charset utf-8;".
2. 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 editor's menu: "File" -> "Save As", you can see the encoding of the current file, make sure the file encoding is: UTF-8,
If it is ANSI, the encoding needs to be changed to: UTF-8.
3.PHP file header BOM problem:
PHP files must not have BOM tags
Otherwise, there will be a situation where the session cannot be used, and there will be a similar prompt:
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 regarded this BOM tag as output, so an error occurred!
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 remove signature",
Then save the file and the BOM tag can be removed!
4.When PHP saves files as attachments, UTF-8 encoding problem:
PHP saves the file as an attachment. The file name must be GB2312 encoded,
Otherwise, if there are Chinese characters in the file name, garbled characters will be displayed:
If your PHP itself is a file in UTF-8 encoding format,
The file name variable needs to be converted from UTF-8 to GB2312:
iconv("UTF-8", "GB2312", "$filename");
5. When truncating and displaying the article title, garbled characters or "?" question marks appear:
Generally, when the article title is very long, part of the title will be displayed and the article title will be truncated,
Since a Chinese character in UTF-8 encoding format will occupy 3 characters in width,
When intercepting the title, sometimes only 1 character or 2 characters of a Chinese character width will be intercepted,
If the interception is not complete, 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;
}
3. The problem of using UTF-8 encoding in MYSQL database
1. Use phpmyadmin to create database and data tables
When creating a database, please set "Organization" 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 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 database
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 the MYSQL database normally.
4. UTF-8 encoding issues related to JS
1. The problem of Chinese garbled characters in JS reading cookies
When writing cookies in PHP, Chinese characters need to be escaped encoded,
Otherwise, the Chinese characters in the cookie read by JS will be garbled.
But PHP itself does not have an escape function. Let’s 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,
Then the problem of Chinese garbled characters in cookies will be solved.
2. UTF-8 encoding problem of external JS files
When an HTML page or PHP page contains an external JS file,
If the HTML page or PHP page is a file in UTF-8 encoding format,
External JS files must also be converted into UTF-8 files,
Otherwise, there will be situations where there is no success and no response when calling the function.
Click the editor's menu: "File" -> "Save As", you can see the encoding of the current file, make sure the file encoding is: UTF-8,
If it is ANSI, the encoding needs to be changed to: UTF-8.
5. UTF-8 encoding issues related to FLASH
FLASH internally processes all strings in UTF-8 by default
1. FLASH reads text files (txt, html)
To save the encoding of text files as UTF-8
Click the editor's menu: "File" -> "Save As", you can see the encoding of the current file, make sure the file encoding is: UTF-8,
If it is ANSI, the encoding needs to be changed to: UTF-8.
2. FLASH reads XML files
To save the encoding of XML files as UTF-8
Click the editor's menu: "File" -> "Save As", you can see the encoding of the current file, make sure the file encoding is: UTF-8,
If it is ANSI, the encoding needs to be changed to: UTF-8.
In XML line 1 write:
3.FLASH reads PHP and returns data
If the PHP encoding itself is UTF-8, just echo it directly
If the PHP encoding itself is GB2312, you can transfer the PHP to a file in UTF-8 encoding format and just echo it directly
If the PHP encoding itself is GB2312 and the encoding format of the file is not allowed to be changed,
Use the following statement to convert the string into UTF-8 encoding format
$new_str = iconv("GB2312", "UTF-8", "$str");
Just echo again
4. FLASH reads data from database (MYSQL)
FLASH needs to read the 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");
5.FLASH writes data through PHP
In a word, the string passed by FLASH is in UTF-8 format,
Convert to the corresponding encoding format and then operate (write files, write databases, display directly, etc.)
Or use the iconv function to convert
6.FLASH uses local encoding (theoretically not recommended)
If you want FLASH not to use UTF-8 encoding, but to use local encoding
For mainland China, the local encoding is GB2312 or GBK
Within the AS program, you can add the following code:
System.useCodepage = true;
Then all characters in FLASH are encoded using GB2312
All data imported to or exported from FLASH should undergo corresponding encoding conversion
Because using local encoding will cause garbled characters for users in Traditional Chinese regions, it is not recommended to use
Excerpted from Gua Xixi