zoukankan      html  css  js  c++  java
  • 去除文件BOM头工具

    <?php
    /**
     * 用法:复制以下代码至新建的php文件中,将该php文件放置项目目录,运行即可。代码来源于网络。
     * chenwei 注。
     */
    header('content-Type: text/html; charset=utf-8');
    $auto=1;/* 设置为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);
    }
  • 相关阅读:
    php插入日期到mysql失败
    CSS Triangle Arrow DIVs tooltilps
    转:javascript对json操作讲解
    bulletproof ajax:ajax 载入时显示动画
    javascript 和ajax创建bookmarket
    css 文字换行
    MySQL数据库在指定位置增加字段
    javascript获取网页URL地址及参数等
    Microsoft JScript runtime error: 'Sys' is undefined 的解决方法
    DetailsView的简单使用
  • 原文地址:https://www.cnblogs.com/farwish/p/3762563.html
Copyright © 2011-2022 走看看