网站进行了升级,有所变化,请使用 搜索 查找更多内容

做网站过程如何移除BOM &#65279

潍坊华邦网络 2016-08-16 09:11:16 1.8K 0

游戏编码:GM393100082 最后修订时间:2016-08-16 09:11:16

无附件

链接如果失效,点此上报

给客户做网站的过程中发现页面顶部总有一个空白区域,使用chrome检查发现,添加了&#65279 bom文件头代码,按照网上的说法,使用程序编辑软件就能移除,使用DW看到已经显示移除,可奇怪的是一只存在,在网上搜索的过程中发现以下代码可以一次全部检测程序代码中的bom,同时可以移除,希望有帮助到同样迷惑的朋友们。

<?php
//remove the utf-8 boms
//by magicbug at gmail dot com
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);
}
?>

评论留言

管理员已经关闭评论
×

扫码登录网站