程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP批量刪除、清除UTF-8文件BOM頭的代碼實例

PHP批量刪除、清除UTF-8文件BOM頭的代碼實例

編輯:關於PHP編程

記得運行代碼前先把文件備份一下哦,避免出現失敗問題。

代碼一:
復制代碼 代碼如下:
   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.");
    }

代碼二:
復制代碼 代碼如下:
<?php
header('content-Type: text/html; charset=utf-8');
if(isset($_GET['dir'])){ //設置文件目錄,如果沒有設置,則自動設置為當前文件所在目錄
    $basedir=$_GET['dir'];
}else{
    $basedir='.';
}
$auto=1;/*設置為1標示檢測BOM並去除,設置為0標示只進行BOM檢測,不去除*/

echo '當前查找的目錄為:'.$basedir.'當前的設置是:';
echo $auto?'檢測文件BOM同時去除檢測到BOM文件的BOM<br />':'只檢測文件BOM不執行去除BOM操作<br />';

checkdir($basedir);
function checkdir($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{
                    $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);
}
?>

代碼三:
復制代碼 代碼如下:
##把該文件放在需求去除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) { // BOM
                                                                                                     // 的前三個字符的ASCII
                                                                                                     // 碼分別為
                                                                                                     // 239
                                                                                                     // 187
                                                                                                     // 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 );
}
?>

 

 

 

 

 

 

 

 

 

 

 

 

 

 


二、Python
復制代碼 代碼如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*-

import os

def delBOM():
 file_count = 0
 bom_files  = []

 for dirpath, dirnames, filenames in os.walk('.'):
  if(len(filenames)):
   for filename in filenames:
    file_count += 1
    file = open(dirpath + "/" + filename, 'r+')
    file_contents = file.read()

    if(len(file_contents) > 3):
     if(ord(file_contents[0]) == 239 and ord(file_contents[1]) == 187 and ord(file_contents[2]) == 191):
      bom_files.append(dirpath + "/" + filename)
      file.seek(0)
      file.write(file_contents[3:])
      print bom_files[-1], "BOM found. Deleted."
    file.close()

 print file_count, "file(s) found.", len(bom_files), "file(s) have a bom. Deleted."

if __name__ == "__main__":
 delBOM()

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved