Almost all PHP developers have encountered the problem of garbled characters. Below we mainly introduce the garbled characters of PHP files and garbled pages.
Problem of converting PHP page to UTF-8 encoding
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 treats the BOM tag as output, so an error occurs!
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 you can remove the BOM tag!
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");
Use a program to demonstrate the character interception method
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 problem of using UTF-8 encoding in MYSQL database", don't miss it.
The above is the solution to the garbled Chinese encoding of UFT-8 in PHP. I hope it will be helpful to everyone's learning.