Home  >  Article  >  Backend Development  >  PHP removes BOM header from utf8 format file_PHP tutorial

PHP removes BOM header from utf8 format file_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:501051browse

We sometimes need to remove the header in utf8 documents and often need to clear it manually. Below I have compiled a few methods to use php programs to remove the BOM header in utf8 format files. I hope it will be helpful to all students.

Example 1

The code is as follows Copy code
 代码如下 复制代码

/**
 * 去掉文件中的 bom头
 * @var 0.1
 * @author Chenwp
 */
function clearbom($contents){   
    //UTF8 去掉文本中的 bom头
    $BOM = chr(239).chr(187).chr(191);
    return str_replace($BOM,'',$contents);   
}

/**
 * 去掉文件中的bom头
 * @param object $fileName Description
 * @return object    Description
 */
function clearfilebom($fileName){
    $c = file_get_contents($fileName);
    $c = clearbom($c);
    file_put_contents($fileName,$c);
}

/**
* Remove the bom header from the file
* @var 0.1
* @author Chenwp
​*/
function clearbom($contents){
//UTF8 removes the BOM header from the text
$BOM = chr(239).chr(187).chr(191);
Return str_replace($BOM,'',$contents);
}

/**
* Remove the BOM header from the file
* @param object $fileName Description
* @return object Description
​*/
function clearfilebom($fileName){
$c = file_get_contents($fileName);
$c = clearbom($c);
File_put_contents($fileName,$c);
}

Example 2
 代码如下 复制代码

//此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除

$basedir="."; //修改此行为需要检测的目录,点表示当前目录
$auto=1; //是否自动移除发现的BOM信息。1为是,0为否。

//以下不用改动

if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file!='.' && $file!='..' && !is_dir($basedir."/".$file)) echo "filename: $file ".checkBOM("$basedir/$file")."
";
       }
       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);
}
//结束
?>


 

How to convert the format with BOM file into unsigned UTF-8 format file? Let me share with you a piece of PHP code:
The code is as follows Copy code
<🎜>//This file is used to quickly test whether the UTF8 encoded file has a BOM added and can be automatically removed <🎜> <🎜>$basedir="."; //Modify the directory that needs to be detected for this behavior. The dot indicates the current directory
$auto=1; //Whether to automatically remove the discovered BOM information. 1 means yes, 0 means no. <🎜> <🎜>//No need to change the following <🎜> <🎜>if ($dh = opendir($basedir)) {
​​​​while (($file = readdir($dh)) !== false) {
if ($file!='.' && $file!='..' && !is_dir($basedir."/".$file)) echo "filename: $file ".checkBOM("$basedir/$file" )."
";
}
       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);
}
//End
?>

Example 3

The code is as follows
 代码如下 复制代码


// 设定你要清除BOM的根目录(会自动扫描所有子目录和文件)
$HOME = dirname(__FILE__);
// 如果是Windows系统,修改为:$WIN = 1;
$WIN = 0;
?>




UTF8 BOM 清除器



$BOMBED = array();
RecursiveFolder($HOME);
echo '

These files had UTF8 BOM, but i cleaned them:

';
foreach ($BOMBED as $utf) { echo $utf ."
n"; }
echo '

';
// 递归扫描
function RecursiveFolder($sHOME) {
 global $BOMBED, $WIN;
 $win32 = ($WIN == 1) ? "" : "/";
 $folder = dir($sHOME);
 $foundfolders = array();
 while ($file = $folder->read()) {
  if($file != "." and $file != "..") {
   if(filetype($sHOME . $win32 . $file) == "dir"){
    $foundfolders[count($foundfolders)] = $sHOME . $win32 . $file;
   } else {
    $content = file_get_contents($sHOME . $win32 . $file);
    $BOM = SearchBOM($content);
    if ($BOM) {
     $BOMBED[count($BOMBED)] = $sHOME . $win32 . $file;
     // 移出BOM信息
     $content = substr($content,3);
     // 写回到原始文件
     file_put_contents($sHOME . $win32 . $file, $content);
    }
   }
  }
 }
 $folder->close();
 if(count($foundfolders) > 0) {
  foreach ($foundfolders as $folder) {
   RecursiveFolder($folder, $win32);
  }
 }
}
// 搜索当前文件是否有BOM
function SearchBOM($string) {
  if(substr($string,0,3) == pack("CCC",0xef,0xbb,0xbf)) return true;
  return false;
}
?>

Copy code

//Set the root directory where you want to clear the BOM (all subdirectories and files will be automatically scanned)
$HOME = dirname(__FILE__);
// If it is a Windows system, change it to: $WIN = 1;
$WIN = 0;
?>




UTF8 BOM Cleaner



$BOMBED = array();
RecursiveFolder($HOME);
echo '

These files had UTF8 BOM, but i cleaned them:

';
foreach ($BOMBED as $utf) { echo $utf ."
n"; }
echo '

';
// Recursive scan
function RecursiveFolder($sHOME) {
global $BOMBED, $WIN;
$win32 = ($WIN == 1) ? "" : "/";
$folder = dir($sHOME);
$foundfolders = array();
while ($file = $folder->read()) {
if($file != "." and $file != "..") {
if(filetype($sHOME . $win32 . $file) == "dir"){
$foundfolders[count($foundfolders)] = $sHOME . $win32 . $file;
} else {
$content = file_get_contents($sHOME . $win32 . $file);
$BOM = SearchBOM($content);
If ($BOM) {
$BOMBED[count($BOMBED)] = $sHOME . $win32 . $file;
//Remove BOM information
$content = substr($content,3);
//Write back to original file
File_put_contents($sHOME . $win32 . $file, $content);
}
}
}
}
$folder->close();
if(count($foundfolders) > 0) {
foreach ($foundfolders as $folder) {
RecursiveFolder($folder, $win32);
}
}
}
// Search if the current file has a BOM
function SearchBOM($string) {
if(substr($string,0,3) == pack("CCC",0xef,0xbb,0xbf)) return true;
return false;
}
?>

http://www.bkjia.com/PHPjc/632745.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632745.html
TechArticle
We sometimes need to remove the header in utf8 documents. We often need to clear it manually. Below I have compiled a few uses How to use PHP program to clear the BOM header in UTF8 format files. I hope this will help everyone...
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