检查php文件中是否含有bom的函数

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

<?php
/*检测并清除BOM*/
if(isset($_GET['dir'])){
$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);
}
}
}//end while
closedir($dh);
}//end if($dh
}//end function
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>");
}
} www.jb51.net
else return ("BOM Not Found.");
}//end function
function rewrite($filename, $data){
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}//end function
?>

相关文章

php使用iconv中文截断问题的解决方法

本文实例讲述了php使用iconv中文截断问题的解决方法。分享给大家供大家参考。具体分析如下: 今天做了一个采集程序,原理很简单,使用curl方法把对方页面的html获取分析,然后正则提...

Windows中安装Apache2和PHP4权威指南

Apache 2和PHP是创建交互式网站的流行方案,而且成本很低。在Windows中安装Apache 2是一件轻而易举的事情,但要使PHP 4与Apache...

PHP+Redis开发的书签案例实战详解

PHP+Redis开发的书签案例实战详解

本文实例讲述了PHP+Redis开发的书签案例。分享给大家供大家参考,具体如下: redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更...

php限制ip地址范围的方法

本文实例讲述了php限制ip地址范围的方法。分享给大家供大家参考。具体如下: 只有在限定范围内的ip地址才能访问 function get_real_ipaddress() { i...

让php处理图片变得简单 基于gb库的图片处理类附实例代码下载

让php处理图片变得简单 基于gb库的图片处理类附实例代码下载

这个类的设计思想借鉴于jQuery,通过连缀方法来操作图片,如: 复制代码 代码如下: $image = new UsaImage(array('filepath'=>'image...