Home > Backend Development > PHP Tutorial > PHP garbled code problem, summary of common problems with UTF-8 garbled code_PHP tutorial

PHP garbled code problem, summary of common problems with UTF-8 garbled code_PHP tutorial

WBOY
Release: 2016-07-21 15:19:17
Original
1155 people have browsed it

1. HTML page conversion to UTF-8 encoding problem
1. Add a line after the head and before the title:


The order cannot be wrong, and the title displayed in

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, sometimes a BOM tag is added at the beginning of the file.
The BOM tag may This will 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 Signature", set it to: "Always remove signature",
then save the file , that is, the BOM label can be removed!
4. WEB server UTF-8 encoding problem:
If you follow the steps listed above and still have Chinese garbled problems,
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 In nginx.conf: charset is set to utf-8.
Specifically find "charset gb2312;" or a similar statement and change it to: "charset utf-8;".
2. PHP page conversion 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, you need to change the encoding to: UTF-8.
3. PHP file header BOM issue:
PHP files must not have BOM tags
Otherwise, the session will not be usable, 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!
So the BOM tag must be deleted on 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 Signature", set it to: "Always remove signature",
then save the file , that is, the BOM label can be removed!
4. UTF-8 encoding problem when PHP saves files as attachments:
PHP saves files as attachments, and the file name must be GB2312 encoded.
Otherwise, if there is Chinese in the file name, It will display 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");

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. Truncate the title of the article.
Since a Chinese character in UTF-8 encoding format will occupy 3 characters of width.
When cutting the title, sometimes only 1 character or 2 characters of width of a Chinese character 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:

Copy code The code is as follows:

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. Problems with using UTF-8 encoding for MYSQL database

1. Use phpmyadmin to create databases and data tables
When creating a 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 this field stores Chinese, you need to "Organization" is set to: "utf8_general_ci",

If the field stores English or numbers, the default is fine.

The corresponding SQL statement, for example:
Copy code The 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 the database

After connecting to the database:

[hide]$connection = mysql_connect($host_name, $host_user, $host_pass);

Add two lines:
Copy code The code is as follows:

mysql_query("set character set 'utf8'" );//Read library
mysql_query("set names 'utf8'");//Write library

and you can read and write the MYSQL database normally.

4. UTF-8 encoding issues related to JS
1. Chinese garbled problem when JS reads cookies

When PHP writes cookies, Chinese characters need to be encoded escape encoding,
otherwise the Chinese characters read by JS in the cookie will be garbled.
But PHP itself does not have an escape function. Let’s write a new escape function:
Copy the code The 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 the cookie, use unescape to decode it.

Then the problem of Chinese garbled characters in the cookie is solved.

2. External JS file UTF-8 encoding issue

When an HTML page or PHP page contains an external JS file,

If the HTML page or PHP page The page is a file in UTF-8 encoding format.

External JS files must also be converted into UTF-8 files.

Otherwise, it will appear that there is no inclusion and there will be no response when calling the function. situation.

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

All strings internally in FLASH are processed in UTF-8 by default
1. FLASH reading Text file (txt, html)
To save the encoding of the text file as UTF-8
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.
2. FLASH reads 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:

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 Yes, you can transfer 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 change the characters Convert the string into UTF-8 encoding format
$new_str = iconv("GB2312", "UTF-8", "$str");
Just echo again
4.FLASH read database ( MYSQL) data
FLASH needs to read the data in the database through PHP
The encoding of PHP itself is not important. The key is that 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 one word, FLASH The passed string is in UTF-8 format.
It needs to be converted into the corresponding encoding format before operation (writing files, writing databases, direct display, 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
AS In the 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 be processed accordingly The encoding conversion
is not recommended because the use of local encoding will cause garbled characters for users in Traditional Chinese regions.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325387.htmlTechArticle1. HTML page conversion to UTF-8 encoding problem 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 displayed...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template