• 去除UTF8 BOM【转】


    utf8的编码有no bom & bom 区分。

    <?php   
    
    if (isset($_GET['dir'])){ //config the basedir   
        $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);   
                    }   
                }   
            }   
        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 found,automatically removed.</font>");   
            } else {   
                return ("<font color=red>BOM found.</font>");   
            }   
        }   
        else return ("BOM Not Found.");   
    }  
    
    function rewrite ($filename, $data) {   
        $filenum = fopen($filename, "w");   
        flock($filenum, LOCK_EX);   
        fwrite($filenum, $data);   
        fclose($filenum);   
    }   
    
    ?> 
  • 相关阅读:
    持久化类的三种状态
    Hibernate持久化类规则
    JSP之Bean
    JSP动作标签
    JSP九大内置对象
    Jsp指令
    JSTL标签语言
    JSP之EL表达式
    Java 中的 Characters
    汇编基本语法
  • 原文地址:https://www.cnblogs.com/eastson/p/2733913.html
Copyright © 2020-2023  润新知