PHP 去掉 utf8格式文件中的bom头部_PHP教程
Jul 13, 2016 am 10:48 AM我们有时要去掉utf8文档中头部我们经常会需要手工清除了,下面我整理了几个利用php程序清除 utf8格式文件中的bom头部方法,希望对各位同学会有所帮助。
例1
代码如下 | 复制代码 |
/** /** |
例2
如何将带有BOM文件的格式转换成无签名的UTF-8格式文件呢?下面分享给大家一段PHP代码:
代码如下 | 复制代码 |
//此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除 $basedir="."; //修改此行为需要检测的目录,点表示当前目录 //以下不用改动 if ($dh = opendir($basedir)) { function checkBOM ($filename) { function rewrite ($filename, $data) {
|
例3
代码如下 | 复制代码 |
$BOMBED = array(); RecursiveFolder($HOME); echo ' These files had UTF8 BOM, but i cleaned them:'; // 递归扫描 function RecursiveFolder($sHOME) { global $BOMBED, $WIN; $win32 = ($WIN == 1) ? "\" : "/"; $folder = dir($sHOME); $foundfolders = array(); while ($file = $folder->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; } ?> |

인기 기사

인기 기사

뜨거운 기사 태그

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법
