thinkphp清除BOM方法

来源:互联网 发布:淘宝客定向计划审核 编辑:程序博客网 时间:2024/05/22 13:49

在utf-8编码文件中BOM在文件头部,占用三个字节,用来标示该文件属于utf-8编码,现在已经有很多软件识别bom头,但是还有些不能识别bom头,比如PHP就不能识别bom头,这也是用记事本编辑utf-8编码后执行就会出错的原因了。BOM只有在WINDOWS下采用“记事本”存储为UTF-8时才会有。

去掉bom头的办法,简单的是下面两种: 

1、editplus去BOM头的方法 
编辑器调整为UTF8编码格式后,保存的文件前面会多出一串隐藏的字符(也即是BOM),用于编辑器识别这个文件是否是以UTF8编码。运行Editplus,点击工具,选择首选项,选中文件,UTF-8标识选择 总是删除签名,然后对PHP文件编辑和保存后的PHP文件就是不带BOM的了。 
2、ultraedit去除bom头办法 
打开文件后,另存为选项的编码格式里选择(utf-8 无bom头),确定就ok了。 
对于PHP程序需要去除Bom头的也可以使用目录下的92wcms.php程序去除。 

请用一下代码运行一次

<?php


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);


}


?> 

原创粉丝点击