zoukankan      html  css  js  c++  java
  • 批量清除BOM头

    批量清除BOM头

     (2012-03-05 13:28:30)
    标签: 

    杂谈

     
    有些php文件由于不小心保存成了含bom头的格式而导致出现一系列的问题。以下是批量清除bom头的代码,复制代码,保存成.php文件,放在想执行的文件夹,运行就可以了
    1. <?php  
    2. if (isset ( $_GET ['dir'] )) { //config the basedir    
    3.   $basedir = $_GET ['dir'];    
    4. } else {    
    5.   $basedir = '.';    
    6. }    
    7.   
    8. $auto = 1;    
    9.   
    10. checkdir ( $basedir );    
    11.   
    12. function checkdir($basedir) {    
    13.   if ($dh = opendir ( $basedir )) {    
    14.     while ( ($file = readdir ( $dh )) !== false ) {    
    15.       if ($file != '.' && $file != '..') {    
    16.         if (! is_dir ( $basedir . "/" . $file )) { // 如果是文件    
    17.           echo "filename: $basedir/$file " . checkBOM ( "$basedir/$file" ) . " <br>";    
    18.         } else {    
    19.           $dirname = $basedir . "/" .$file; // 如果是目录    
    20.           checkdir ( $dirname ); // 递归    
    21.         }    
    22.       }    
    23.     }    
    24.     closedir ( $dh );    
    25.   }    
    26. }    
    27.   
    28. function checkBOM($filename) {    
    29.   global $auto;    
    30.   $contents = file_get_contents ( $filename );    
    31.   $charset [1] = substr ( $contents, 0, 1 );    
    32.   $charset [2] = substr ( $contents, 1, 1 );    
    33.   $charset [3] = substr ( $contents, 2, 1 );    
    34.   if (ord ( $charset [1] ) == 239 && ord ( $charset [2] ) == 187 && ord ( $charset [3] ) == 191) { // BOM 的前三个字符的ASCII 码分别为 239 187 191    
    35.     if ($auto == 1) {    
    36.       $rest = substr ( $contents, 3 );    
    37.       rewrite ( $filename, $rest );    
    38.       return ("<font color=red>BOM found, automatically removed.</font>");    
    39.     } else {    
    40.       return ("<font color=red>BOM found.</font>");    
    41.     }    
    42.   } else    
    43.     return ("BOM Not Found.");    
    44. }    
    45.   
    46. function rewrite($filename, $data) {    
    47.   $filenum = fopen ( $filename, "w" );    
    48.   flock ( $filenum, LOCK_EX );    
    49.   fwrite ( $filenum, $data );    
    50.   fclose ( $filenum );    
  • 相关阅读:
    哈希表
    java读写xml文件
    Java学习之Hessian通信基础
    DevExpress 中 gridView编辑单元格,失去焦点后,内容继而消失
    DevExpress控件的GridControl控件小结
    Spring 架构图
    WebLogic和Tomcat的区别
    EJB到底是什么,真的那么神秘吗??
    C# 枚举类型
    关于C#的Main(String[] args)参数输入问题
  • 原文地址:https://www.cnblogs.com/u0mo5/p/4629753.html
Copyright © 2011-2022 走看看