您好,欢迎访问一九零五行业门户网

如何使用PHP批量去除文件UTF8 BOM信息_PHP教程

原理:
utf8文件,微软为了增加一个识别信息,有了bom这个东西:bom —— byte order mark,缺省在windows等平台上编辑的utf8文件会在头部增加3个字节的标记信息,我们php引擎在处理的时候会完整读取整个php代码文档, 如果php文件头部包含bom信息,就会输出一个空白,在很多时候会带来问题,比如我们session无法工作、cookie无法设置等等问题。
解决方法:
把头部bom的3个字节信息识别出来,然后剔除掉。不过一般情况我们不知道哪个文件有bom,或者是有很多文件,这个时候,就需要进行批量处理了,下面代码主要就是展现了批量处理的情况,应该会对大家工作中有帮助。
执行方法:
设置一个路径,然后直接执行就行。
复制代码 代码如下:
utf8 bom 清除器
read()) {
  if($file != . and $file != ..) {
   if(filetype($shome . $win32 . $file) == dir){
    $foundfolders[count($foundfolders)] = $shome . $win32 . $file;
   } else {
    $content = file_get_contents($shome . $win32 . $file);
    $bom = searchbom($content);
    if ($bom) {
     $bombed[count($bombed)] = $shome . $win32 . $file;
     // 移出bom信息
     $content = substr($content,3);
     // 写回到原始文件
     file_put_contents($shome . $win32 . $file, $content);
    }
   }
  }
 }
 $folder->close();
 if(count($foundfolders) > 0) {
  foreach ($foundfolders as $folder) {
   recursivefolder($folder, $win32);
  }
 }
}
// 搜索当前文件是否有bom
function searchbom($string) {
  if(substr($string,0,3) == pack(ccc,0xef,0xbb,0xbf)) return true;
  return false;
}
?>
http://www.bkjia.com/phpjc/328077.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/328077.htmltecharticle原理: utf8文件,微软为了增加一个识别信息,有了bom这个东西:bom —— byte order mark,缺省在windows等平台上编辑的utf8文件会在头部增加...
其它类似信息

推荐信息