記得運行代碼前先把文件備份一下哦,避免出現失敗問題。
代碼一:
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."); }
代碼二:
<?php header('content-Type: text/html; charset=utf-8'); if(isset($_GET['dir'])){ //設置文件目錄,如果沒有設置,則自動設置為當前文件所在目錄 $basedir=$_GET['dir']; }else{ $basedir='.'; } $auto=1;/*設置為1標示檢測BOM並去除,設置為0標示只進行BOM檢測,不去除*/ echo '當前查找的目錄為:'.$basedir.'當前的設置是:'; echo $auto?'檢測文件BOM同時去除檢測到BOM文件的BOM<br />':'只檢測文件BOM不執行去除BOM操作<br />'; checkdir($basedir); function checkdir($basedir){ if($dh=opendir($basedir)){ while (($file=readdir($dh)) !== false){ if($file != '.' && $file != '..'){ if(!is_dir($basedir.'/'.$file)){ echo '文件: '.$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並已自動去除</font>'); }else{ return (' <font color=red>找到BOM</font>'); } }else{ return (' 沒有找到BOM'); } } function rewrite($filename,$data){ $filenum=fopen($filename,'w'); flock($filenum,LOCK_EX); fwrite($filenum,$data); fclose($filenum); } ?>
代碼三:
##把該文件放在需求去除BOM頭的目錄下跑一下卻可。 <?php 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) { // BOM // 的前三個字符的ASCII // 碼分別為 // 239 // 187 // 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 ); } ?>
二、Python
#!/usr/bin/env python #-*- coding: utf-8 -*- import os def delBOM(): file_count = 0 bom_files = [] for dirpath, dirnames, filenames in os.walk('.'): if(len(filenames)): for filename in filenames: file_count += 1 file = open(dirpath + "/" + filename, 'r+') file_contents = file.read() if(len(file_contents) > 3): if(ord(file_contents[0]) == 239 and ord(file_contents[1]) == 187 and ord(file_contents[2]) == 191): bom_files.append(dirpath + "/" + filename) file.seek(0) file.write(file_contents[3:]) print bom_files[-1], "BOM found. Deleted." file.close() print file_count, "file(s) found.", len(bom_files), "file(s) have a bom. Deleted." if __name__ == "__main__": delBOM()
為了方便大家使用,這裡小編分享一個BOM工具方便大家檢測。
下載地址:http://www.jb51.net/softs/496779.html