您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP 去掉 utf8格式文件中的bom头部_PHP教程

2026/1/28 2:21:32发布21次查看
我们有时要去掉utf8文档中头部我们经常会需要手工清除了,下面我整理了几个利用php程序清除 utf8格式文件中的bom头部方法,希望对各位同学会有所帮助。
例1
 代码如下 复制代码
/**
 * 去掉文件中的 bom头
 * @var 0.1
 * @author chenwp
 */
function clearbom($contents){   
    //utf8 去掉文本中的 bom头
    $bom = chr(239).chr(187).chr(191);
    return str_replace($bom,'',$contents);   
}
/**
 * 去掉文件中的bom头
 * @param object $filename description
 * @return object    description
 */
function clearfilebom($filename){
    $c = file_get_contents($filename);
    $c = clearbom($c);
    file_put_contents($filename,$c);
}
例2
如何将带有bom文件的格式转换成无签名的utf-8格式文件呢?下面分享给大家一段php代码:
 代码如下 复制代码
例3
 代码如下 复制代码
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/632745.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/632745.htmltecharticle我们有时要去掉utf8文档中头部我们经常会需要手工清除了,下面我整理了几个利用php程序清除 utf8格式文件中的bom头部方法,希望对各位同...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product