一键去除网页BOM属性【解决乱码,头部空白,&#65279问题】

886次阅读
没有评论

几个常出现的问题:

1. 网站打开空白  

2. 页面头部出现多余的空白  

3. 网站出现乱码,如“锘�” 

解决方法可以是:

1. 选用专业的编辑器,例如 notepad++,sublime,editplus 这样不会自动签名。 

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

3.notepad++ 选中格式 -> 以 UTF- 8 格式编码 选项即可去除

<?php 
if (isset($_GET['dir'])){ // 设置文件目录
$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); 
} 
?>

使用方法:复制上面代码到一个新建的 delbom.txt 的文件中,上传到网站根目录,然后将 delbom.txt 改为 delbom.php,最后直接前台访问该文件即可。

正文完
 0
飞翔的mouse
版权声明:本站原创文章,由 飞翔的mouse 于2020-03-30发表,共计1252字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。