Created
August 27, 2014 01:21
-
-
Save clinyong/55ddb24e559c67d8af1d to your computer and use it in GitHub Desktop.
I use this script to detect bom file recursively, and delete bom with sublime text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 用法:复制以下代码至新建的php文件中,将该php文件放置项目目录,运行即可。代码来源于网络。 | |
* chenwei 注。 | |
*/ | |
header('content-Type: text/html; charset=utf-8'); | |
$auto=0;/* 设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除 */ | |
$basedir='.'; | |
$loop=true; | |
echo '当前查找的目录为:'.$basedir.'当前的设置是:'; | |
echo '(1)',$loop?'检查当前目录以及当前目录的子目录':'只针对当前目录进行检测'; | |
echo '(2)',$auto?'检测文件BOM同时去除检测到BOM文件的BOM<br />':'只检测文件BOM不执行去除BOM操作<br />'; | |
checkdir($basedir,$loop); | |
function checkdir($basedir='',$loop=true){ | |
$basedir=empty($basedir)?'.':$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{ | |
if(!$loop) continue; | |
$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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment