用PHP解决万恶的BOM_PHP教程

WBOY
Freigeben: 2016-07-14 10:12:01
Original
866 Leute haben es durchsucht

[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)  
    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); 
        } 
    } 

$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)

 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文件,使用前需备份

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477261.htmlTechArticle[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); i...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!