检查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针对字符串开头和结尾的判断方法

本文实例讲述了PHP针对字符串开头和结尾的判断方法。分享给大家供大家参考,具体如下: //变量: $s1 = "dotcoo world!"; $s2 = "dotcoo"; $s3...

Php Cookie的一个使用注意点

复制代码 代码如下:<?php setcookie('test', 'this is a cookie test'); echo ($_COOKIE['test']); ?>...

php中explode的负数limit用法分析

本文实例讲述了php中explode的负数limit用法。分享给大家供大家参考。具体分析如下: explode -- 使用一个字符串分割另一个字符串,使之成为数组。 参数为: arr...

php mongodb操作类 带几个简单的例子

之前【宜配屋www.yipeiwu.com】已经发过几篇类似的文章,大家可以参考一下。 核心代码: class NewMongodb { private $mongo; /...

用PHP代码给图片加水印

用PHP代码给图片加水印

先找好一张图片,更名为face.jpeg,创建watermark.php: <?php /** * Created by PhpStorm. * User: A...