加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

批量去除PHP文件中bom的PHP代码

发布时间:2020-12-13 06:05:24 所属栏目:PHP教程 来源:网络整理
导读:需要去除BOM,就把附件里的tool.php文件放到目标目录,然后在浏览器访问tool.php即可! div class="codetitle" a style="CURSOR: pointer" data="87539" class="copybut" id="copybut87539" onclick="doCopy('code87539')" 代码如下: div class="codebody" i

需要去除BOM,就把附件里的tool.php文件放到目标目录,然后在浏览器访问tool.php即可!
<div class="codetitle"><a style="CURSOR: pointer" data="87539" class="copybut" id="copybut87539" onclick="doCopy('code87539')"> 代码如下:<div class="codebody" id="code87539">
<?php
//此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除
$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")."
";
}
closedir($dh);
}
function checkBOM ($filename) {
global $auto;
$contents=file_get_contents($filename);
$charset[1]=substr($contents,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 ("BOM found,automatically removed.");
} else {
return ("BOM found.");
}
}else
return ("BOM Not Found.");
}
function rewrite ($filename,$data) {
$filenum=fopen($filename,"w");
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
?>

PHP批量去除PHP文件中bom的代码
<div class="codetitle"><a style="CURSOR: pointer" data="79253" class="copybut" id="copybut79253" onclick="doCopy('code79253')"> 代码如下:<div class="codebody" id="code79253">
<?php
if (isset($_GET['dir'])){ //设置文件目录
$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")."
";
}else{
$dirname = $basedir."/".$file;
checkdir($dirname);
}
}
}
closedir($dh);
}
}
function checkBOM ($filename) {
global $auto;
$contents = file_getcontents($filename);
$charset[1] = substr($contents,1);
$charset[2] = substr($contents,1);
$charset[3] = substr($contents,1);
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
if ($auto == 1) {
$rest = substr($contents,automatically removed.
http://www.joyphper.net");
} else {
return ("BOM found.");
}
}
else return ("BOM Not Found.");
}
function rewrite ($filename,$data) {
$filenum = fopen($filename,$data);
fclose($filenum);
}
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读