PHP garbled problem, UTF-8 (garbled)

PHP garbled problem, UTF-8 (garbled)


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


<meta http-equiv='Content-Type' content='text/html; charset =utf-8' /> The
order can not be wrong, it must be


displayed in the 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, you need to change the encoding to: UTF-8. 
3. HTML file header BOM problem: 
When converting a file from other encodings to UTF-8 encoding, sometimes a BOM tag is added at the beginning of the file. A 
BOM tag may cause the browser to display Chinese when it is displayed. Garbled characters appear. 
The method to delete this BOM tag: 
1. You can open the file with Dreamweaver and save it again, that is, you can remove the BOM tag! 
2. You can open the file with EditPlus, and in the menu "Preferences" -> "File" -> "UTF-8 Logo", set it to: "Always delete signature", 
then save the file, you can remove the BOM tag! 
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: charset in the configuration file to: utf-8 (only the methods are listed here, please refer to the configuration file of apache for the specific format) 
If you are using Nginx, please add nginx.conf to : charset is set to utf-8, 
find "charset gb2312;" or a similar statement, and change it to: "charset utf-8;".
2. Problems with converting PHP pages to UTF-8 encoding 
1. Add a line at the beginning of the code: 
header("Content-Type: text/html;charset=utf-8"); 2.


Problems with encoding PHP files


"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 cannot be used, and a similar prompt will appear: 
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 there is a BOM tag in the previous PHP page, 
PHP regards this BOM tag as output, so an error occurs! 
So the PHP page must delete the BOM tag 
The method to delete this BOM tag: 
1. You can open the file with Dreamweaver and save it again, that is, you can remove the BOM tag! 
2. You can open the file with EditPlus, and in the menu "Preferences" -> "File" -> "UTF-8 Logo", set it to: "Always delete signature", 
then save the file, you can remove the BOM tag! 
4. When PHP saves files as attachments, UTF-8 encoding problem: 
PHP saves files as attachments, the file name must be GB2312 encoding, 
otherwise, if there is Chinese in the file name, it will display garbled characters: 
If your PHP It is a file in UTF-8 encoding format, and 
the file name variable needs to be converted from UTF-8 to GB2312: 
iconv("UTF-8", "GB2312", "$filename"); 


5. When the title of the article is truncated, it appears The problem of garbled characters or "?" question marks: 
Generally, when the title of the article is very long, a 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 width, 
when the title is intercepted , sometimes only 1 character or 2 characters width of a Chinese character will be intercepted  . If
the interception is not complete, there will be garbled characters or "?" question marks. 
Use the following function to intercept the title, there will be no 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. MYSQL database using UTF-8 encoding


 


1. When creating a database and data table with phpmyadmin 
, please set the "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 is to store Chinese, you need to set the "collation" to: "utf8_general_ci",


if the field is to store English or numbers, The default is fine.


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. Use PHP 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 the library


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


, you can read and write the MYSQL database normally.


 


 


4. JS-related UTF-8 encoding problems 
1. The Chinese garbled problem of JS reading cookies 


 


When PHP writes a cookie, it needs to escape encoding of Chinese characters, 
otherwise the Chinese characters in the cookie read by JS will be garbled. 
But php itself does not have an escape function, 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 reads cookies At the same time, use unescape to decode,


and then solve the problem of Chinese garbled characters in cookies.


2. The problem of UTF-8 encoding 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, the


external JS file should also be converted into UTF-8 files,


Otherwise, there will be no containment unsuccessful, no response when the function is called.


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.


5. FLASH-related UTF-8 encoding problems


FLASH internally processes all strings in UTF- 

by 
default 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. 
2. FLASH reads the XML file. 
To save the encoding of the XML file as UTF-8 
, click the menu of the editor: "File" -> "Save As", you can see the encoding of the current file, make sure that the encoding of the file is: UTF-8, 
If it is ANSI, you need to change the encoding to: UTF-8. 
Write in the first line of XML: 


3.FLASH reads the data returned by PHP 
. If the PHP encoding itself is UTF-8, you can directly echo it. 
If the PHP encoding itself is GB2312, you can transfer PHP to a file in UTF-8 encoding format. , just echo directly.
If  PHP encoding itself is GB2312, and it is not allowed to change the encoding format of the file,
use the following statement to convert the string to UTF-8 encoding format 
$new_str = iconv("GB2312", "UTF-8", "$str");  and  then
echo will  do  Not important, the key is that if the database encoding is GB2312, you  need to use the following statement to convert the string to UTF-8 encoding format  $new_str = iconv("GB2312", "UTF-8", "$str");  5. FLASH writes data through PHP. In  one sentence, the string passed by FLASH is in UTF-8 format. It  needs to be converted into the corresponding encoding format, and then operated (writing files, writing databases, direct display, etc.)  or using the iconv function to convert  6. FLASH uses local encoding (not recommended in theory)  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 program, you can add the following code :  System.useCodepage = true;  then all characters in FLASH are encoded using GB2312.  All data imported into or exported from FLASH should be converted to the corresponding encoding  because the use of local encoding will cause users in traditional Chinese regions Generates garbled characters, so it is not recommended to use


















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325616670&siteId=291194637