PHP page to UTF-8 Chinese code garbled solution _php Tips

Source: Internet
Author: User

For garbled this problem almost all of the PHP developers have encountered, we are mainly introduced below the PHP file garbled and garbled page.

PHP page UTF-8 encoding problem
1. Add a line to 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 encoding to ensure that the file is encoded as: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
3.PHP file Header BOM problem:
The PHP file must not have a BOM label
Otherwise, the session cannot be used, and there is a similar hint:
Warning:session_start () [Function.session-start]: Cannot send session cache Limiter-headers already sent
This is because, in the execution of Session_Start (), the entire page can not have output, but when the previous PHP page exists BOM label, PHP to this BOM label as output, so it went wrong!
So php page must delete BOM label!!!
How to delete this BOM label:
1. You can open the file with Dreamweaver and save it again, that is, you can remove the BOM label!
2. You can open the file with EditPlus, and in the menu "preferences"-> "file"-> "UTF-8 identity", set to: "Always delete signature",
Then save the file, that is, you can remove the BOM label!
4.PHP when saving files as an attachment, the UTF-8 encoding problem:
PHP as an attachment to save the file, file name must be GB2312 code, otherwise, if the file name in Chinese, will be displayed garbled:
If your PHP itself is a UTF-8 encoded file, you need to turn the filename variable from UTF-8 to GB2312:
iconv ("UTF-8", "GB2312", "$filename");
Using program to instance 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 "MySQL database using UTF-8 coding problem", we do not miss.

The above is about PHP uft-8 Chinese code garbled solution, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.