PHP中出现BOM字符\ufeff,PHP去掉诡异的BOM \ufeff

转自:http://blog.csdn.net/fansunion/article/details/45540001

  研究一个PHP项目的时候,今天项目突然打不开了。

  前几天还好好的,用Chrome看了下Response的内容,AJAX页面和普通HTML页面内容前面有一个红色的点。

 鼠标移上去,提示“\ufeff”。


 百度下,发现是BOM字符。


  前几天还好好的,难道是因为今天替换某些内容,导致的?


 从网上找了个php程序,扫描所有的文件,把BOM字符去掉。


 妈蛋,为啥总是有这么多不同的标准。


代码:

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. header('content-Type: text/html; charset=utf-8');  
  3. if(isset($_GET['dir'])){ //设置文件目录,如果没有设置,则自动设置为当前文件所在目录  
  4.     $basedir=$_GET['dir'];  
  5. }else{  
  6.     $basedir='.';  
  7. }  
  8. $auto=1;/*设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除*/  
  9.   
  10. echo '当前查找的目录为:'.$basedir.'当前的设置是:';  
  11. echo $auto?'检测文件BOM同时去除检测到BOM文件的BOM<br />':'只检测文件BOM不执行去除BOM操作<br />';  
  12.   
  13. checkdir($basedir);  
  14. function checkdir($basedir){  
  15.     if($dh=opendir($basedir)){  
  16.         while (($file=readdir($dh)) !== false){  
  17.             if($file != '.' && $file != '..'){  
  18.                 if(!is_dir($basedir.'/'.$file)){  
  19.                     echo '文件: '.$basedir.'/'.$file .checkBOM($basedir.'/'.$file).' <br>';  
  20.                 }else{  
  21.                     $dirname=$basedir.'/'.$file;  
  22.                     checkdir($dirname);  
  23.                 }  
  24.             }  
  25.         }  
  26.         closedir($dh);  
  27.     }  
  28. }  
  29. function checkBOM($filename){  
  30.     global $auto;  
  31.     $contents=file_get_contents($filename);  
  32.     $charset[1]=substr($contents,0,1);  
  33.     $charset[2]=substr($contents,1,1);  
  34.     $charset[3]=substr($contents,2,1);  
  35.     if(ord($charset[1])==239 && ord($charset[2])==187 && ord($charset[3])==191){  
  36.         if($auto==1){  
  37.             $rest=substr($contents,3);  
  38.             rewrite($filename,$rest);  
  39.             return (' <font color=red>找到BOM并已自动去除</font>');  
  40.         }else{  
  41.             return (' <font color=red>找到BOM</font>');  
  42.         }  
  43.     }else{  
  44.         return (' 没有找到BOM');  
  45.     }  
  46. }  
  47. function rewrite($filename,$data){  
  48.     $filenum=fopen($filename,'w');  
  49.     flock($filenum,LOCK_EX);  
  50.     fwrite($filenum,$data);  
  51.     fclose($filenum);  
  52. }  
  53. ?>  

代码来自:http://www.douban.com/note/332229277/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值