Heim  >  Artikel  >  Backend-Entwicklung  >  怎么去掉utf-8 的bom头?

怎么去掉utf-8 的bom头?

WBOY
WBOYOriginal
2016-06-20 12:43:391409Durchsuche

谷歌,360浏览器,QQ浏览器中网页顶部有bom头;火狐浏览器显示正常。
我试了好多种办法(Dreamweaver,Notepad++,网上搜了个去除bom的程序文件),最终还是没能解决,请问各位大神还有什么办法吗?

这是谷歌浏览器中显示bom图的部分:


回复讨论(解决方案)

文件本身有汉字的,直接另存为 无BOM urf-8 编码
文件本身没有汉字的,直接另存为 ANSI 编码

从多语言版考虑,程序文件中是不应该之间特定语种的文字的。而是在运行时刻通过字典充填内容的

文件本身有汉字的,直接另存为 无BOM urf-8 编码
文件本身没有汉字的,直接另存为 ANSI 编码

从多语言版考虑,程序文件中是不应该之间特定语种的文字的。而是在运行时刻通过字典充填内容的



我就是用编辑器保存为无bom的utf-8编码,还是不行,但是在火狐浏览器中就正常。这是为什么?

所有相关的文件都做了吗?

不是所有的浏览器都能忽略 BOM 头的

如果安装了netbeans,用它打开文件,
如果文件有bom头,会在第一行最前面 显示一个小点,删除这个小点,保存就可以了

所有相关的文件都做了吗?

不是所有的浏览器都能忽略 BOM 头的



这是我的程序
<?phpheader('content-Type: text/html; charset=utf-8');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); }

我用这个去除bom也不行

你这个程序对删除 utf-8 BOM 是有效的

你可以这样看看

$s = file_get_contents('http://localhost');echo bin2hex($s);
贴出输出的前20个字符

你这个程序对删除 utf-8 BOM 是有效的

你可以这样看看

$s = file_get_contents('http://localhost');echo bin2hex($s);
贴出输出的前20个字符


这段代码放在bom程序文件里还是?

从你的截图上看,有  字样
这应该是 unicode 的 BOM 头,要判断文件的头2个字符的内码值是否是 255 和 254

那段代码单独现在一个程序里,执行

从你的截图上看,有  字样
这应该是 unicode 的 BOM 头,要判断文件的头2个字符的内码值是否是 255 和 254


输出从第一个开始

那段代码单独现在一个程序里,执行


这个是单独执行的

你 #10 的截图中有 3 个 utf-8 BOM
你 #11 的截图中没有 BOM

你 #10 的截图中有 3 个 utf-8 BOM
你 #11 的截图中没有 BOM



看不懂,咋解决了

要去掉所有先关文件中的 bom
你的那个程序放的位置很重要,再说你可能还引用了网站外的文件

要去掉所有先关文件中的 bom
你的那个程序放的位置很重要,再说你可能还引用了网站外的文件

我的bom程序就放在项目根目录,和其它文件是兄弟关系。

如果你将项目中的文件的 bom 都去除了,页面中却还有 bom 存在的话
那么就表示你还使用了项目以外的文件

如果你将项目中的文件的 bom 都去除了,页面中却还有 bom 存在的话
那么就表示你还使用了项目以外的文件


找的我都头大了,还是找不出来,谢谢你了

那你就把 bom程序 放到网站的根上运行

那你就把 bom程序 放到网站的根上运行



我怀疑这跟一些js有关系,网站里有些程序文件,都是js写的

难道你认为 js 文件不是 相关文件 吗?

难道你认为 js 文件不是 相关文件 吗?




刚我把网站首页里那些js代码去掉了还是不行

难道你认为 js 文件不是 相关文件 吗?


因为这段js中的路径是hm.src = "//hm.baidu.com/hm.js?54403e32d263feb8a8a2d4a630ccc73f";
误以为跟它有关

你的网站在公网上吗?贴出网址来

你的网站在公网上吗?贴出网址来


http://www.25mj.com/

有 3 个 bom
请贴出 index.php

有 3 个 bom
请贴出 index.php

<?php	include("inc.php");	?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "w3.org/TR/html4/strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta name="baidu-site-verification" content="koeyExgBxq" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /><title>[东方美居官方网站]中国领先的装修-建材-家居领域电子商务网站,专业装修建材家具消费导购平台</title><meta name="keywords" content="东方美居,建材家具,西安建材团购,建材团购,装修团购,建材市场,团购建材,家居建材" /><meta name="description" content="东方美居建材灯饰广场,领先的装修,建材,家居领域电子商务网站,是最专业,最权威的西安建材团购、西安家具团购、装修设计团购网站,致力于为装修业主提供更便宜、方便、放心的装修消费方式,成为专业的装修建材家具消费导购平台" /><link rel="Shortcut Icon" href="favicon.ico" ><link rel="Bookmark" href="favicon.ico" ><link rel="icon" href="favicon.ico" type="image/x-icon" /><link rel="stylesheet" type="text/css" href="css/home.css"><script src="js/swfobject_modified.js" type="text/javascript"></script><script>var _hmt = _hmt || [];(function() {  var hm = document.createElement("script");  hm.src = "//hm.baidu.com/hm.js?54403e32d263feb8a8a2d4a630ccc73f";  var s = document.getElementsByTagName("script")[0];   s.parentNode.insertBefore(hm, s);})();</script><script type="text/javascript" src="js/jquery.js"></script>	<script type="text/javascript"> $(function(){     $("#btn_phone").click(function(){         var xingming = $("#name").val();        var dianhua = $("#phone").val();    	if((dianhua.length != 0) && (xingming.length != 0)){    	    if(!(/^1(?:[38]\d|4[57]|5[0123456789])\d{8}$/.test(dianhua))){     	        alert("不是完整的11位手机号或者不正确的手机号前七位");     	        document.feedback.dianhua.focus();     	        return false;    	    }else{    	    	var param = 'name='+xingming+'&phone='+dianhua;    	    	$.ajax({    	    		url:"setPersonAdd.php",    	    		data:param,    	    		type:"POST",    	    		success:function(data){        	    		if(data.trim() != null){	    	    			alert(data);	    	    			window.location.href="./index.php";             	    	}    	    		}    	    	});    		}     	       	}else{    	    if (xingming.length == 0){    	       alert("姓名不能为空!");    	       document.feedback.name.focus();    	       return false;    	     }    	     if(dianhua.length == 0){    			alert("电话不能为空!");    			document.feedback.phone.focus();    		    return false;    		  }    	}    }); });</script><script type="text/javascript">$(document).ready(function() {	//近期活动	$("#jq").mouseover(function(){		$("#jc").removeClass("jq");		$("#jj").removeClass("jq");		$("#zx").removeClass("jq");		$("#fl").removeClass("jq");				$('#jiancai').hide();		$('#jiaju').hide();		$('#zhuangxiu').hide();		$('#fuliao').hide();		$("#jq").addClass("jq");         $("#jinqihuodong").show();    }).mouseout(function(e){    	if(($("#jq").attr("class")) != "jq"){	    	$('#jinqihuodong').hide();        }    });	//找建材	$("#jc").mouseover(function(){		$("#jq").removeClass("jq");		$("#jj").removeClass("jq");		$("#zx").removeClass("jq");		$("#fl").removeClass("jq");		$('#jinqihuodong').hide();		$('#jiaju').hide();		$('#zhuangxiu').hide();		$('#fuliao').hide();				$("#jc").addClass("jq");          $("#jiancai").show();    }).mouseout(function(e){    	if(($("#jc").attr("class")) != "jq"){    		$('#jiancai').hide();        }    });	//找家具	$("#jj").mouseover(function(){		$("#jc").removeClass("jq"); 		$("#jq").removeClass("jq");		$("#zx").removeClass("jq");		$("#fl").removeClass("jq"); 		$('#jinqihuodong').hide();		$('#jiancai').hide();		$('#zhuangxiu').hide();		$('#fuliao').hide();		$("#jj").addClass("jq");          $("#jiaju").show();    }).mouseout(function(e){    	if(($("#jj").attr("class")) != "jq"){    		$('#jiaju').hide();        }    });	//找装修	$("#zx").mouseover(function(){		$("#jj").removeClass("jq");		$("#jc").removeClass("jq"); 		$("#jq").removeClass("jq");		$("#fl").removeClass("jq");		$('#jinqihuodong').hide();		$('#jiancai').hide();		$('#fuliao').hide();		$('#jiaju').hide();		   		$("#zx").addClass("jq");         $("#zhuangxiu").show();    }).mouseout(function(e){    	if(($("#zx").attr("class")) != "jq"){    		$('#zhuangxiu').hide();        }    });	//找辅料	$("#fl").mouseover(function(){		$("#jq").removeClass("jq");		$("#jj").removeClass("jq");		$("#jc").removeClass("jq"); 		$("#zx").removeClass("jq");		$('#jinqihuodong').hide();		$('#jiancai').hide();		$('#zhuangxiu').hide();		$('#jiaju').hide();				$("#fl").addClass("jq");          $("#fuliao").show();    }).mouseout(function(e){    	if(($("#fl").attr("class")) != "jq"){    		$('#fuliao').hide();        }    });});</script><script type="text/javascript">$(document).ready(function() {	//近期活动	$(".home03_nr ul li").each(function(i){		$('#foreman_'+i).mouseover(function(){	        $('#foreman_detail_'+i).show();	    }).mouseout(function(e){		    $('#foreman_detail_'+i).hide();	    });			});});</script><script type="text/javascript">$(document).ready(function() {	//金牌工长	$("#gz").mouseover(function(){		$("#gj").removeClass("jphbs");		$("#sj").removeClass("jphbs");			$('#jpgj').hide();		$('#jpsj').hide();				$("#gz").addClass("jphbs");  	    $("#jpgz").show();	}).mouseout(function(e){		if(($("#gz").attr("class")) != "jphbs"){			$('#jpgz').hide();	    }	});	//金牌管家	$("#gj").mouseover(function(){		$("#gz").removeClass("jphbs");		$("#sj").removeClass("jphbs");			$('#jpgz').hide();		$('#jpsj').hide();				$("#gj").addClass("jphbs");  	    $("#jpgj").show();	}).mouseout(function(e){		if(($("#gj").attr("class")) != "jphbs"){			$('#jpgj').hide();	    }	});	//金牌设计	$("#sj").mouseover(function(){		$("#gz").removeClass("jphbs");		$("#gj").removeClass("jphbs");			$('#jpgz').hide();		$('#jpgj').hide();				$("#sj").addClass("jphbs");  	    $("#jpsj").show();	}).mouseout(function(e){		if(($("#sj").attr("class")) != "jphbs"){			$('#jpsj').hide();	    }	});	//建材类	$("#jcl").mouseover(function(){		$("#jjl").removeClass("zxhbs");		$("#fll").removeClass("zxhbs");			$('#jiajulei').hide();		$('#fuliaolei').hide();				$("#jcl").addClass("zxhbs");  	    $("#jiancailei").show();	}).mouseout(function(e){		if(($("#jcl").attr("class")) != "zxhbs"){			$('#jiancailei').hide();	    }	});	//家具类	$("#jjl").mouseover(function(){		$("#jcl").removeClass("zxhbs");		$("#fll").removeClass("zxhbs");			$('#jiancailei').hide();		$('#fuliaolei').hide();				$("#jjl").addClass("zxhbs");  	    $("#jiajulei").show();	}).mouseout(function(e){		if(($("#jjl").attr("class")) != "zxhbs"){			$('#jiajulei').hide();	    }	});	//辅料类	$("#fll").mouseover(function(){		$("#jcl").removeClass("zxhbs");		$("#jjl").removeClass("zxhbs");			$('#jiancailei').hide();		$('#jiajulei').hide();				$("#fll").addClass("zxhbs");  	    $("#fuliaolei").show();	}).mouseout(function(e){		if(($("#fll").attr("class")) != "zxhbs"){			$('#fuliaolei').hide();	    }	});});</script><link type="text/css" href="css/thickbox.css" rel="stylesheet" /><script type="text/javascript" src="js/jquery-1.2.6.pack.js"></script><script type="text/javascript" src="js/thickbox.js"></script><link type="text/css" rel="stylesheet" href="css/lrtk.css" /><script type="text/javascript" src="js/pptBox.js"></script></head><body>    	<div class="home_login">        <div class="home_margin">        	<div class="home_l_l"><img  src="images/ico01.jpg"    style="max-width:90%"  style="max-width:90%" / alt="怎么去掉utf-8 的bom头?" ><span>欢迎来到东方美居建材灯饰广场</span></div>            <div class="home_l_r">            	<a href="login.php">会员登录</a>                <a href="register.php">会员注册</a>                <a href="#">个人中心</a>            </div>          </div>        </div>		<div class="home_margin">        <div class="home_top">       <div style="float:left; margin-top:29px;"><img  src="images/logo.jpg"  / alt="怎么去掉utf-8 的bom头?" ></div>       <div class="home_soso"><input name="" type="text" /><a href="#"></a></div>       <div   style="max-width:90%"><img  src="images/tel.jpg"  / alt="怎么去掉utf-8 的bom头?" ></div>               </div></div><div class="bfb_menu">  <div class="home_margin"> <div class="home_menu">		<a href="http://www.25mj.com/">首 页</a>        <a href="http://www.25mj.com/huodong/151107/index.php?status=0" target="_blank">找家具</a>         <a href="http://www.25mj.com/huodong/151107/index.php?status=0" target="_blank">找建材</a>                 <a href="http://www.25mj.com/huodong/151107/index.php?status=0" target="_blank">找辅料</a>           <a href="http://www.25mj.com/luntan/luntanindex.php" target="_blank">美居之家</a>  </div>	  </div></div></div>

太长了,我分开,这是第一部分

有 3 个 bom
请贴出 index.php



总共要6万多字节,你说贴哪块我贴出来

$url = 'http://www.25mj.com/';echo bin2hex(file_get_contents($url, false, null, 0, 20)), PHP_EOL;echo bin2hex(file_get_contents($url . 'inc.php', false, null, 0, 20));

efbbbfefbbbfefbbbf3c21444f43545950452048
efbbbfefbbbf

访问你的 index.php,返回了 3 个 bom
访问你的 inc.php,返回了 2个 bom
显然你的 index.php、inc.php 以及在 inc.php 中 include 的一个文件中 有 bom

你是怎么执行你的 去除bom程序 的?
还是只放上去了没有执行?

$url = 'http://www.25mj.com/';echo bin2hex(file_get_contents($url, false, null, 0, 20)), PHP_EOL;echo bin2hex(file_get_contents($url . 'inc.php', false, null, 0, 20));

efbbbfefbbbfefbbbf3c21444f43545950452048
efbbbfefbbbf

访问你的 index.php,返回了 3 个 bom
访问你的 inc.php,返回了 2个 bom
显然你的 index.php、inc.php 以及在 inc.php 中 include 的一个文件中 有 bom

你是怎么执行你的 去除bom程序 的?
还是只放上去了没有执行?



我的bom程序就在http://www.25mj.com/delete_bom.php  这里执行的,这里就是根目录

$url = 'http://www.25mj.com/';echo bin2hex(file_get_contents($url, false, null, 0, 20)), PHP_EOL;echo bin2hex(file_get_contents($url . 'inc.php', false, null, 0, 20));

efbbbfefbbbfefbbbf3c21444f43545950452048
efbbbfefbbbf

访问你的 index.php,返回了 3 个 bom
访问你的 inc.php,返回了 2个 bom
显然你的 index.php、inc.php 以及在 inc.php 中 include 的一个文件中 有 bom

你是怎么执行你的 去除bom程序 的?
还是只放上去了没有执行?


我执行bom程序,页面没有显示找index.php文件的bom记录,难道bom程序没有执行index.php吗?

$url = 'http://www.25mj.com/';echo bin2hex(file_get_contents($url, false, null, 0, 20)), PHP_EOL;echo bin2hex(file_get_contents($url . 'inc.php', false, null, 0, 20));

efbbbfefbbbfefbbbf3c21444f43545950452048
efbbbfefbbbf

访问你的 index.php,返回了 3 个 bom
访问你的 inc.php,返回了 2个 bom
显然你的 index.php、inc.php 以及在 inc.php 中 include 的一个文件中 有 bom

你是怎么执行你的 去除bom程序 的?
还是只放上去了没有执行?


目前运行index.php显示是这样的

你的程序是怎么放到服务器上去的?
你不能在本地处理好以后再上传吗?

$url = 'http://www.25mj.com/';echo bin2hex(file_get_contents($url, false, null, 0, 20)), PHP_EOL;echo bin2hex(file_get_contents($url . 'inc.php', false, null, 0, 20));

efbbbfefbbbfefbbbf3c21444f43545950452048
efbbbfefbbbf

访问你的 index.php,返回了 3 个 bom
访问你的 inc.php,返回了 2个 bom
显然你的 index.php、inc.php 以及在 inc.php 中 include 的一个文件中 有 bom

你是怎么执行你的 去除bom程序 的?
还是只放上去了没有执行?



这里显示已找到bom,并已清除,但是运行index.php还是有bom。
运行结果如楼上所示。

你的程序是怎么放到服务器上去的?
你不能在本地处理好以后再上传吗?



我的程序就是在服务器上改的,我的bom程序也是我ftp传上去的,然后http://www.25mj.com/delete_bom.php运行。

$url = 'http://www.25mj.com/';
echo bin2hex(file_get_contents($url . 'index.php')), PHP_EOL;
可以见到 3 个 bom
但是你的 http://www.25mj.com/delete_bom.php 的报告中并没有这个文件!报告中的 index.php 都在子目录中

你把 delete_bom.php 中的 rewrite 函数改成这样:

function rewrite ($filename, $data) {   if($filenum = fopen($filename, "w")) {        if(flock($filenum, LOCK_EX)) {          if(! fwrite($filenum, $data)) echo  "$filename 写入失败<br>";       }else echo  "$filename 锁定失败<br>";       fclose($filenum);    }else echo "$filename 打开失败<br>";  }
看看是否是没有改写成功

你把 delete_bom.php 中的 rewrite 函数改成这样:

function rewrite ($filename, $data) {   if($filenum = fopen($filename, "w")) {        if(flock($filenum, LOCK_EX)) {          if(! fwrite($filenum, $data)) echo  "$filename 写入失败<br>";       }else echo  "$filename 锁定失败<br>";       fclose($filenum);    }else echo "$filename 打开失败<br>";  }
看看是否是没有改写成功


我改成这个之后运行bom,就显示
这样,bom还在。

这就对了!文件都打不开,怎么去掉 bom 头?
你把那些打不开的文件下载下来,改好后重新传上去

这就对了!文件都打不开,怎么去掉 bom 头?
你把那些打不开的文件下载下来,改好后重新传上去


奥,怪不得了

这就对了!文件都打不开,怎么去掉 bom 头?
你把那些打不开的文件下载下来,改好后重新传上去


这是为什么呢?难道我在服务器上的权限还不够?

文件是只读的,就是这样

你的文件不仅是在网站管理下,还在 ftp 管理下。这种情况是经常发生的

写权限实际是由 ftp 控制的,你可尝试用 ftp 客户端修改文件权限

写权限实际是由 ftp 控制的,你可尝试用 ftp 客户端修改文件权限



懂了,谢谢了,终于解决了。
Stellungnahme:
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