一鍵去除網頁BOM屬性【解決亂碼,頭部空白,驗證碼不顯示問題】

發佈時間:2019-09-29

幾個常出現的問題:

1.網站打開空白

2.頁面頭部出現多餘的空白

3.網站出現亂碼,如“鍩�”

4.後台登錄驗證碼不顯示 


解決方案:

1.選用專業的編輯器,例如notepad++,sublime,editplus這樣不會自動簽名。

2.sublime通過如下操作File -> Save with Encoding -> UTF-8保存後即可去除bom

3.notepad++選中格式 -> 以UTF-8格式編碼 選項即可去除

4.用如下方法可一次性清理網頁所有文本文件的bom 

複製下面代碼

<?php  
$dir=dirname(__FILE__);

if(isset($_GET["act"]) && $_GET["act"] == "del" ){   //Del the file!!
$filename=basename($_SERVER['PHP_SELF']);
@unlink($filename);
exit('Error:File Not Found!');
}

echo "The document:".$dir."&nbsp;&nbsp;<a href=\"?act=del\" >[Del the delbom.php file]</a><BR/>";

if (!is_dir($dir))
die("$dir not exists");
$auto = 1;  
checkdir($dir);  
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/>\n";  
    }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>\n");  
  } else {  
   return ("<font color=red>BOM found.</font>\n");  
  }  
}  
else return ("BOM Not Found.\n");  
}  
function rewrite ($filename, $data) {  
$filenum = fopen($filename, "w");  
flock($filenum, LOCK_EX);  
fwrite($filenum, $data);  
fclose($filenum);  
}  
?>
複製上面的代碼

使用方法:複製上面代碼到一個新建的delbom.php(用記事本或者是文本編輯器打開此文件)的文件中,上傳到網站根目錄,然後訪問您的站點,比如http://www.**.com/delbom.php 即可清理所有網頁中存在的bom便簽。