PHP scrambled code problem UTF 8 scrambled code common problems summary

  • 2020-05-16 06:34:34
  • OfStack

1.HTML page to UTF-8 encoding problem
1. After head, add 1 line before title:

< meta http-equiv='Content-Type' content='text/html; charset=utf-8' / >
You can't go in the wrong order. You have to go in 1

Show the title may be garbled!

2.html file encoding:

Click on the editor's menu: "file" - > "Save as", you can see the code of the current file, make sure the file code is: UTF-8,
If it's ANSI, change the code to UTF-8.
3.HTML header BOM question:
When converting files from other encodings to UTF-8, sometimes an BOM tag is added to the beginning of the file,
An BOM TAB may cause the browser to display garbled Chinese.
Delete this BOM tag method:
1. You can open the file with Dreamweaver and save it again, which means you can remove the BOM tag!
2. You can open the file with EditPlus and go to "preferences" - in the menu > "File" > "UTF-8 logo", set to "always delete signature",
Then save the file so you can remove the BOM tag!
4.WEB server UTF-8 encoding problem:
If you follow the steps listed above, you still have the problem of Chinese garbled codes.
Please check the encoding of the WEB server you are using
If you are using Apache, set: charset in the configuration file to: utf-8 (only methods are listed here, please refer to the apache configuration file for the specific format).
If you are using Nginx, set charset in nginx.conf to utf-8,
Find "charset gb2312;" Or something like that: "charset utf-8;" .
2.PHP page to UTF-8 encoding problem
1. Add 1 line at the beginning of the code:
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 code, make sure the file code is: UTF-8,
If it is ANSI, change the code to UTF-8.
3.PHP header BOM question:
PHP file 1 must not have an BOM label
Otherwise, the session will not work, with a similar prompt:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent
This is because when session_start() is executed, the entire page cannot have output, but when the BOM tag is present on the previous PHP page,
PHP mistook the BOM tag for output, so there was an error!
So PHP page 1 must remove the BOM tag
Delete this BOM tag method:
1. You can open the file with Dreamweaver and save it again. You can remove the BOM tag!
2. You can use EditPlus to open the file and in the menu "preferences" - > "File" > "UTF-8 logo", set to "always delete signature",
Then save the file so you can remove the BOM tag!
4. When PHP saves the file as an attachment, UTF-8 encoding problem:
PHP saves the file as an attachment, the file name must be GB2312,
Otherwise, if there is Chinese in the file name, it will display garbled code:
If your PHP itself is an UTF-8 encoded file,
The filename variable needs to be converted from UTF-8 to GB2312:
iconv("UTF-8", "GB2312", "$filename");

5. When the title of the article is truncated, garbled code or "? "appears. Question mark:
1. When the title of a general article is very long, 1 part of the title will be displayed, and the title of the article will be truncated.
Since one UTF-8 Chinese character takes up three character widths,
When intercepting a title, sometimes only one or two characters wide of one Chinese character will be truncated.
If you do not intercept completely, you will see scrambled code or "?" The question mark case,
Intercepting the title with the following function should not be a problem:
 
function get_brief_str($str, $max_length) 
{ 
echo strlen($str) ."<br>"; 
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. Problem of MYSQL database using UTF-8 encoding

1. Create databases and tables with phpmyadmin
When creating the database, set "collation" to "utf8_general_ci"
Or execute statement:

CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
When creating the data table: if this field is in Chinese, you need to set "collation" to "utf8_general_ci",

If the field is in English or Numbers, the default is ok.

The 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. Read and write to the database using PHP

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 the library  
mysql_query("set names 'utf8'");// Write a library  

Can read and write MYSQL database normally.

4. UTF-8 coding issues related to JS
1.JS read Cookie

PHP needs to encode Chinese characters escape when writing cookie,
Otherwise JS will read the Chinese characters in cookie in a garbled way.
However, php itself has no escape function, so we 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); 
} 

JS, when you read cookie, you decode it with unescape,

Then we will solve the problem of Chinese garbled codes in cookie.

2. External JS file UTF-8 encoding problem

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

If the HTML page or PHP page is an UTF-8 encoded file,

The external JS file will also be converted to the UTF-8 file,

Otherwise, there will be a situation in which there is no response when the function is called without success.

Click on the editor's menu: "file" - > "Save as", you can see the code of the current file, make sure the file code is UTF-8,

If it is ANSI, change the code to UTF-8.

5.FLASH - related UTF-8 coding issues

FLASH internally handles all strings with UTF-8 by default
1. This document (txt,html)
To save the encoding of the text file as UTF-8
Click on the editor's menu: "file" - > "Save as", you can see the code of the current file, make sure the file code is: UTF-8,
If it is ANSI, change the code to UTF-8.
2.FLASH reads XML files
To save the encoding of the XML file as UTF-8
Click on the editor's menu: "file" - > "Save as", you can see the code of the current file, make sure the file code is: UTF-8,
If it is ANSI, change the code to UTF-8.
In line 1 of XML write:

3.FLASH read PHP return data
If the PHP code itself is UTF-8, echo will do
If the PHP code itself is GB2312, you can transfer PHP to UTF-8, and echo directly will do
If the PHP encoding itself is GB2312 and it is not allowed to change the encoding format of the file,
Convert the string to the encoding of UTF-8 with the following statement
$new_str = iconv("GB2312", "UTF-8", "$str");
Just echo
4.FLASH reads data from the database (MYSQL)
FLASH reads data from the database through PHP
The encoding of PHP itself is not important. The point is that if the encoding of the database is GB2312,
You need to convert the string to the UTF-8 encoding format with the following statement
$new_str = iconv("GB2312", "UTF-8", "$str");

5.FLASH writes data through PHP
1 sentence, FLASH sends a string in UTF-8,
To convert to the appropriate encoding format, and then operate (write to files, write to databases, direct display, etc.)
Again, iconv
6.FLASH USES local encoding (not recommended in theory)
If you want FLASH to use local encoding instead of UTF-8
For mainland China, the local code is GB2312 or GBK
The following code can be added to the AS program:
System.useCodepage = true;
So all the characters in FLASH are encoded with GB2312
All data imported into or exported from FLASH should be encoded accordingly
Because the use of local coding, will cause users in traditional Chinese areas to produce garbled code, so it is not recommended to use

Related articles: