zoukankan      html  css  js  c++  java
  • 检测php文件是否有bom头

    <?
    02    //此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除
    03    //By Bob Shen
    04    
    05    $basedir="."; //修改此行为需要检测的目录,点表示当前目录
    06    $auto=1; //是否自动移除发现的BOM信息。1为是,0为否。
    07    
    08    //以下不用改动
    09    
    10    if ($dh = opendir($basedir)) {
    11    while (($file = readdir($dh)) !== false) {
    12    if ($file!='.' && $file!='..' && !is_dir($basedir."/".$file)) echo "filename: $file ".checkBOM("$basedir/$file")." <br>";
    13    }
    14    closedir($dh);
    15    }
    16    
    17    function checkBOM ($filename) {
    18    global $auto;
    19    $contents=file_get_contents($filename);
    20    $charset[1]=substr($contents, 0, 1);
    21    $charset[2]=substr($contents, 1, 1);
    22    $charset[3]=substr($contents, 2, 1);
    23    if (ord($charset[1])==239 && ord($charset[2])==187 && ord($charset[3])==191) {
    24    if ($auto==1) {
    25    $rest=substr($contents, 3);
    26    rewrite ($filename, $rest);
    27    return ("<font color=red>BOM found, automatically removed.</font>");
    28    } else {
    29    return ("<font color=red>BOM found.</font>");
    30    }
    31    }
    32    else return ("BOM Not Found.");
    33    }
    34    
    35    function rewrite ($filename, $data) {
    36    $filenum=fopen($filename,"w");
    37    flock($filenum,LOCK_EX);
    38    fwrite($filenum,$data);
    39    fclose($filenum);
    40    }

  • 相关阅读:
    守望先锋2中源氏皮肤变化
    博客园页面定制设置背景
    京剧中的“八句唱法”
    长歌行
    长相知-《上邪》-汉乐府
    Eclipse离线安装svn插件
    不安装Oracle客户端,用PLSQL连接Oracle
    常见数据库默认端口以及常用数据类型
    Python2.7 删除前N天日志文件
    Anaconda用conda创建python虚拟环境
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2891547.html
Copyright © 2011-2022 走看看