转自:http://blog.csdn.net/fansunion/article/details/45540001
研究一个PHP项目的时候,今天项目突然打不开了。
前几天还好好的,用Chrome看了下Response的内容,AJAX页面和普通HTML页面内容前面有一个红色的点。
鼠标移上去,提示“\ufeff”。
百度下,发现是BOM字符。
前几天还好好的,难道是因为今天替换某些内容,导致的?
从网上找了个php程序,扫描所有的文件,把BOM字符去掉。
妈蛋,为啥总是有这么多不同的标准。
代码:
- <?php
- header('content-Type: text/html; charset=utf-8');
- if(isset($_GET['dir'])){
- $basedir=$_GET['dir'];
- }else{
- $basedir='.';
- }
- $auto=1;
-
- echo '当前查找的目录为:'.$basedir.'当前的设置是:';
- echo $auto?'检测文件BOM同时去除检测到BOM文件的BOM<br />':'只检测文件BOM不执行去除BOM操作<br />';
-
- 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).' <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并已自动去除</font>');
- }else{
- return (' <font color=red>找到BOM</font>');
- }
- }else{
- return (' 没有找到BOM');
- }
- }
- function rewrite($filename,$data){
- $filenum=fopen($filename,'w');
- flock($filenum,LOCK_EX);
- fwrite($filenum,$data);
- fclose($filenum);
- }
- ?>
代码来自:http://www.douban.com/note/332229277/