搜索
首页php教程PHP源码PHP批量检测并去除文件BOM头信息代码

因为文件头信息输出BOM头信息,有时会对程序的执行结果造成影响,那么此时即应对这些文件的BOM信息进行去除。

<script>ec(2);</script>

如下代码为PHP方式去除当前目录及子目录所有文件BOM信息的代码,新建文件,将其放倒根目录下,然后浏览器访问即可。

例子

将以上代码保存为后缀为php的文件放到需要去除bom的文件目录里面,然后运行该php文件,将会对该目录以及该目录所有的子目录下的文件进行bom检查并去除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") . "
";
                } 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 ("BOM found, automatically removed._http://www.111cn.net");
        } 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);
}
?>

例子二

 代码如下 复制代码

header('content-Type: text/html; charset=utf-8');
$auto=1;/*设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除*/
$basedir='.';
$loop=true;//www.111cn.net echo '当前查找的目录为:'.$basedir.'当前的设置是:';
echo '(1)',$loop?'检查当前目录以及当前目录的子目录':'只针对当前目录进行检测';
echo '(2)',$auto?'检测文件BOM同时去除检测到BOM文件的BOM
':'只检测文件BOM不执行去除BOM操作
';

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).'
';
    }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 (' 找到BOM并已自动去除');
  }else{
   return (' 找到BOM');
  }
 }else{
  return (' 没有找到BOM');
 }
}
function rewrite($filename,$data){
 $filenum=fopen($filename,'w');
 flock($filenum,LOCK_EX);
 fwrite($filenum,$data);
 fclose($filenum);
}

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。