Home  >  Article  >  Backend Development  >  Invisible character 65279 (BOM header of utf-8) problem in PHP, 65279bom_PHP tutorial

Invisible character 65279 (BOM header of utf-8) problem in PHP, 65279bom_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:20:45954browse

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

Today on CSDN I saw a problem with PHP outputting the blank invisible character 65279. I looked it up on the Internet and found that the 65279 character is used by PHP to mark the file as UTF-8 encoded. When outputting, it will be output to the client together. end, causing the client to be unable to match the string when using ajax to get the return value.

php invisible character 65279 is explained as follows:

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

What is BOM?

 "EF BB BF" These three bytes are called BOM, and the full name is "Byte Order Mard". BOM is commonly used in utf8 files to indicate that the file is a UTF-8 file, and the original intention of BOM is to be used in utf16.

When a UTF-8 file is output in PHP, BOM will be output, so to use UTF-8 in PHP, you must use a UTF-8 file without BOM header.

Commonly used text editing software supports different ways of saving UTF-8 files, so you should pay special attention when using them.

For example:

1. When using ultraedit, there are two options: "UTF-8" and "UTF-8 - No BOM" when saving.
2. The notepad in window saves the file with BOM.
3. Different versions of EditPlus software have different support for saving UTF-8. For example: version 2.31 saves without BOM, and version 2.11 saves with BOM.

How to remove the utf-8 file header:

1. Use ultraedit to save as "UTF-8 - No BOM"
2. A very useful PHP program, run in the root directory of the site, will remove the BOM headers of all UTF-8 files in the directory. 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")." <br>"; 
         }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 ("<font color=red>BOM found, automatically removed.</font>"); 
     } else { 
       return ("<font color=red>BOM found.</font>"); 
     } 
   } 
   else return ("BOM Not Found."); 
 } 
  
 function rewrite ($filename, $data) {
   $filenum = fopen($filename, "w"); 
   flock($filenum, LOCK_EX); 
   fwrite($filenum, $data); 
   fclose($filenum); 
} 

The problem of PHP program UTF8 without BOM, please enlighten me

First, BOM will not affect the decoding you display...
If you manually decode, it will work normally. That's because you didn't specify the 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 use (utf-8 encoding format) or (utf-8 no BOM encoding format)

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

When PHP processes the BOM header, sometimes there are errors, which may cause an error that the file has been output when you use functions such as header or session_start. Most of the time it is because the BOM header is sent out. . Because in php's view, it becomes a space. So use BOM-free format!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/864933.htmlTechArticleInvisible character 65279 (utf-8 BOM header) problem in php, 65279bom I saw a php output in CSDN today I looked up the problem of the blank invisible character 65279 on the Internet and found that the 65279 character was sent by PHP...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn