This article mainly introduces the php uft-8 Chinese encoding garbled solution, need friends can refer to the problem of garbled php developers will almost have encountered, the following describes php file garbled text and page garbled text.
PHP page to UTF-8 encoding problems
1. add a line at the beginning of the code:
Header ("Content-Type: text/html; charset = utf-8 ");
2. php file encoding problems
Click the editor menu: "File"-> "save as", you can see the current file encoding, make sure the file encoding: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
3. php file header BOM problems:
The PHP file cannot have BOM labels.
Otherwise, the session cannot be used, and a similar prompt is displayed:
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 be output. However, because the former PHP page has a BOM label, PHP regards this BOM label as output, so an error occurred!
Therefore, you must delete the BOM label on the PHP page !!!
To delete this BOM tag:
1). you can use Dreamweaver to open the file and save it again, removing the BOM tag!
2). you can open the file with EditPlus, and in the menu "preferences"-> "files"-> "UTF-8 identity", set to "always delete signature ",
Save the file to remove the BOM label!
4. UTF-8 coding problems when PHP saves files in the form of attachments:
PHP saves the file as an attachment. the file name must be GB2312 encoded. Otherwise, if the file name contains Chinese characters, it will be garbled:
If your PHP itself is a UTF-8-encoded file, you need to convert the file name variable from the UTF-8 to GB2312:
Iconv ("UTF-8", "GB2312", "$ filename ");
How to use programs to intercept instance characters
function utf8_substr($str,$len) { for($i=0;$i<$len;$i++) { $temp_str=substr($str,0,1); if(ord($temp_str) > 127){ $i++; if($i<$len){ $new_str[]=substr($str,0,3); $str=substr($str,3); } }else { $new_str[]=substr($str,0,1); $str=substr($str,1); } } return join($new_str); }
The following article focuses on the use of MYSQL database UTF-8 coding issues, do not miss it.
The above is about php uft-8 Chinese encoding garbled solution, hope to help everyone to learn.