PHP Invisible character 65279 (utf-8 BOM header) problem, 65279bom_php tutorial

Source: Internet
Author: User
Tags ultraedit


PHP Invisible character 65279 (utf-8 BOM header) problem, 65279bom




Today in csdn see a PHP output blank stealth character 65279 problem, found on the Internet, sent under the 65279 character PHP is used to mark the file is UTF-8 encoded, output will be output to the client, resulting in the client if using AJAX to get the return value, The string could not be matched.



PHP Stealth character 65279 is explained as follows:



UTF-8 encoded files can be divided into two formats without BOM and BOM.



What is a BOM?



The three-byte "EF BB BF" is called a BOM, and the full name is "Byte Order Mard". The usual BOM in the UTF8 file indicates that the file is a UTF-8 file, and the BOM is intended to be used in UTF16.



The Utf-8 file is output in PHP when the BOM is output, so to use Utf-8 in PHP, you must use the Utf-8 file without the BOM header.



Commonly used text editing software to save the Utf-8 file is not the same way to use the time to pay special attention.



For example:



1, when using UltraEdit, there will be "UTF-8" and "UTF-8-no BOM" two options when saving.
2. Window's notepad is saved with BOM.
3, EditPlus software different version of the Utf-8 save support is not the same, for example: 2.31 version of the Save is not with the BOM, 2.11 version is saved with the BOM.



The way to remove the Utf-8 file header:



1, use UltraEdit save, select "UTF-8-No BOM"
2, a very useful PHP program, put in the site root directory to run, will be the directory of all the Utf-8 file BOM header removed, the code is as follows:


//remove the utf-8 boms 
 //by magicbug at gmail dot com 
  
 if (isset($_GET['dir'])){ //config the basedir 
   $basedir=$_GET['dir']; 
 }else{ 
   $basedir = '.'; 
 } 
  
 $auto = 1; 
  
 checkdir($basedir); 
  
 function checkdir($basedir){ 
   if ($dh = opendir($basedir)) { 
     while (($file = readdir($dh)) !== false) { 
       if ($file != '.' && $file != '..'){ 
         if (!is_dir($basedir."/".$file)) { 
           echo "filename
 $basedir/$file ".checkBOM("$basedir/$file")." 
"; 
         }else{ 
           $dirname = $basedir."/".$file; 
           checkdir($dirname); 
         } 
       } 
     } 
   closedir($dh); 
   } 
 } 
  
 function checkBOM ($filename) { 
   global $auto; 
   $contents = file_get_contents($filename); 
   $charset[1] = substr($contents, 0, 1); 
   $charset[2] = substr($contents, 1, 1); 
   $charset[3] = substr($contents, 2, 1); 
   if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) { 
     if ($auto == 1) { 
       $rest = substr($contents, 3); 
       rewrite ($filename, $rest); 
       return ("BOM found, automatically removed."); 
     } else { 
       return ("BOM found."); 
     } 
   } 
   else return ("BOM Not Found."); 
 } 
  
 function rewrite ($filename, $data) {
   $filenum = fopen($filename, "w"); 
   flock($filenum, LOCK_EX); 
   fwrite($filenum, $data); 
   fclose($filenum); 
} 

PHP program UTF8 No BOM issues, Enlighten


First, the BOM is not affected by the Yi code you display ...
If you manually yi the code, it will be OK. That's because you didn't specify a string encoding in your file.
Header ("Content-type:text/html;charset=utf8"), add this sentence at the beginning of the PHP file



When encoding php files with Utf-8, we can choose to (utf-8 encoded format) or (utf-8 no BOM encoding format)



When saving, use UTF-8 no BOM encoding format.

PHP in the processing of the BOM header, sometimes there are errors, may cause you to use the header or Session_Start and other functions, the file has been output errors, most of all because the BOM header sent out. Because in PHP's view, it's a space. So use a no BOM format!



http://www.bkjia.com/PHPjc/864933.html www.bkjia.com true http://www.bkjia.com/PHPjc/864933.html techarticle php in the invisible character 65279 (utf-8 BOM head) problem, 65279bom today in csdn see a PHP output blank stealth character 65279 problem, found on the Internet, the next 65279 characters are PHP with ...

Related Article

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.