今天为一个客户解决网页模板问题发现一个有意思的问题,客户还是对代码有些小懂,自己尝试着改了一下,结果发现头部多出来一个空行,找到我以后,随即f12查看了一下,一看乐了,出现了个字符把头部的位置给挤下去了,查询了一些资料发现,这个问题其实很好解决,主要原因其实是客户使用了txt文本文档对模板文件进行了编辑,好多新手都存在这个问题,asp时代已经过去了,想当初,一个txt文本文档行走天下,现在这个是用不了了。现在将解决办法分享给大家!
方法1:
直接notopad++ 保存为无dom格式 (格式->转为UTF-8 无dom格式)
方法2:
自己生成个bom清除工具,直接放在网站根目录,运行即可。下面为bom代码:
header('content-Type: text/html; charset=utf-8');
if(isset($_GET['dir'])){ //设置文件目录,如果没有设置,则自动设置为当前文件所在目录
$basedir=$_GET['dir'];
}else{
$basedir='.';
}
$auto=1;/*设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除*/
echo '当前查找的目录为:'.$basedir.'当前的设置是:';
echo $auto?'检测文件BOM同时去除检测到BOM文件的BOM
':'只检测文件BOM不执行去除BOM操作
';
checkdir($basedir);
function checkdir($basedir){
if($dh=opendir($basedir)){
while (($file=readdir($dh)) !== false){
if($file != '.' && $file != '..'){
if(!is_dir($basedir.'/'.$file)){
echo '文件: '.$basedir.'/'.$file .checkBOM($basedir.'/'.$file).'
';
}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 (' 找到BOM并已自动去除');
}else{
return (' 找到BOM');
}
}else{
return (' 没有找到BOM');
}
}
function rewrite($filename,$data){
$filenum=fopen($filename,'w');
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
?>
复制以上代码,然后另存为一个名为bom.php的文件,然后放置在网站服务器或虚拟主机根目录下,运行http://www.xx.com/bom.php即可清除产生的bom头格式。