用PHP去掉文件头的Unicode签名(BOM)方法

yipeiwu_com6年前PHP代码库

废话不多说,直接上代码

<?php

//此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除

//By Bob Shen

$basedir="."; //修改此行为需要检测的目录,点表示当前目录

$auto=1; //是否自动移除发现的BOM信息。1为是,0为否。

//以下不用改动

if ($dh = opendir($basedir)) {

while (($file = readdir($dh)) !== false) {

if ($file!='.' && $file!='..' && !is_dir($basedir."/".$file)) echo "filename: $file ".checkBOM("$basedir/$file")." <br>";

}

closedir($dh);

}

function checkBOM ($filename) {

$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) {
$rest=substr($contents, 3);
rewrite ($filename, $rest);
return true;
}
else return ("BOM Not Found.");
}

function rewrite ($filename, $data) {
$filenum=fopen($filename,"w");
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}

?>

以上这篇用PHP去掉文件头的Unicode签名(BOM)方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【宜配屋www.yipeiwu.com】。

相关文章

php中curl、fsocket、file_get_content三个函数的使用比较

抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简...

PHP中cookie知识点学习

什么是cookie cookie,即小饼干,是保存在用户代理端(浏览器是最常见的用户代理)的一些数据片段。浏览网页时,浏览器会将 当前页面有效的 cookie放在请求的头部发送到服务端。...

使用php shell命令合并图片的代码

复制代码 代码如下: #!/usr/local/bin/php -q author:freemouse <?php // 下面是说明. print ("本程序用于合并2张 640x...

php中get_defined_constants函数用法实例分析

本文实例讲述了php中get_defined_constants函数用法。分享给大家供大家参考。具体分析如下: get_defined_constants ( PHP 4中 > =...

PHP-FPM的配置与优化讲解

PHP-FPM(FastCGI Process Manager:FastCGI进程管理器)是一个PHPFastCGI管理器,用于管理 PHP 进程池、接收和处理 Web 服务器的请求。P...