程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 用PHP解決萬惡的BOM

用PHP解決萬惡的BOM

編輯:關於PHP編程

[php]
<?php 
$path = dirname(__FILE__); 
 
scandDir($path); 
 
 
function bomtoutf8($filename) 

    $ef = 239; 
    $bb = 187; 
    $bf = 191; 
 
    if (is_dir($filename)) return; 
    $fd = fopen($filename, "rb"); 
    if (!$fd) exit($filename); 
    $headers = fread($fd, 3); 
    fclose($fd); 
     
    if (strlen($headers) < 3) return; 
 
    if (ord($headers[0]) == $ef &&  
        ord($headers[1]) == $bb && 
        ord($headers[2]) == $bf 
        ) { 
        echo $filename, "\n"; 
        $content = file_get_contents($filename); 
        if (!$content) { 
            exit("file get contents failed"); 
        } 
        $len = strlen($content); 
        $content = substr($content, 3); 
 
        if ($content === false && $len!=3) { 
            exit("sbustr failed"); 
        } else if ($content === false && $len==3){ 
            $content = ""; 
        } 
        $ret = file_put_contents($filename, $content, LOCK_EX); 
        if ($ret === false) { 
            exit("file put contents failed"); 
        } 
        echo "successed file ", $filename, "\n"; 
    } 
 
 

 
function scandDir($path) 

    $d = dir($path); 
    if (!$d) exit("dir failed"); 
 
    while (false !== ($entry = $d->read())) { 
        if ($entry == '.' || $entry == '..') { 
            continue; 
        } 
        $filename = $path . DIRECTORY_SEPARATOR . $entry; 
    //  echo $filename, "\n";  
        if (is_dir($filename)) { 
            scandDir($filename); 
        } else { 
            bomtoutf8($filename); 
        } 
    } 

<?php
$path = dirname(__FILE__);

scandDir($path);


function bomtoutf8($filename)
{
 $ef = 239;
 $bb = 187;
 $bf = 191;

 if (is_dir($filename)) return;
 $fd = fopen($filename, "rb");
 if (!$fd) exit($filename);
 $headers = fread($fd, 3);
 fclose($fd);
 
 if (strlen($headers) < 3) return;

 if (ord($headers[0]) == $ef &&
  ord($headers[1]) == $bb &&
  ord($headers[2]) == $bf
  ) {
  echo $filename, "\n";
  $content = file_get_contents($filename);
  if (!$content) {
   exit("file get contents failed");
  }
  $len = strlen($content);
  $content = substr($content, 3);

  if ($content === false && $len!=3) {
   exit("sbustr failed");
  } else if ($content === false && $len==3){
   $content = "";
  }
  $ret = file_put_contents($filename, $content, LOCK_EX);
  if ($ret === false) {
   exit("file put contents failed");
  }
  echo "successed file ", $filename, "\n";
 }


}

function scandDir($path)
{
 $d = dir($path);
 if (!$d) exit("dir failed");

 while (false !== ($entry = $d->read())) {
  if ($entry == '.' || $entry == '..') {
   continue;
  }
  $filename = $path . DIRECTORY_SEPARATOR . $entry;
 // echo $filename, "\n";
  if (is_dir($filename)) {
   scandDir($filename);
  } else {
   bomtoutf8($filename);
  }
 }
}


友情提示:代碼是批量處理目錄下(包括子目錄)的所有帶BOM文件,使用前需備份

 

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