批量去除PHP文件中bom的PHP代碼
更新時(shí)間:2012年03月13日 23:41:01 作者:
今天搜索查看網(wǎng)頁源碼時(shí)為什么開頭會(huì)有空行這個(gè)問題時(shí)找到的,批量去除PHP文件中bom的PHP代碼
需要去除BOM,就把附件里的tool.php文件放到目標(biāo)目錄,然后在瀏覽器訪問tool.php即可!
<?php
//此文件用于快速測(cè)試UTF8編碼的文件是不是加了BOM,并可自動(dòng)移除
$basedir="."; //修改此行為需要檢測(cè)的目錄,點(diǎn)表示當(dāng)前目錄
$auto=1; //是否自動(dòng)移除發(fā)現(xiàn)的BOM信息。1為是,0為否。
//以下不用改動(dòng)
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) {
global $auto;
$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) {
if ($auto==1) {
$rest=substr($contents, 3);
rewrite ($filename, $rest);
return ("<font color=red>BOM found, automatically removed.</font>");
} else {
return ("<font color=red>BOM found.</font>");
}
}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的代碼
<?php
if (isset($_GET['dir'])){ //設(shè)置文件目錄
$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")." <br>";
}else{
$dirname = $basedir."/".$file;
checkdir($dirname);
}
}
}
closedir($dh);
}
}
function checkBOM ($filename) {
global $auto;
$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) {
if ($auto == 1) {
$rest = substr($contents, 3);
rewrite ($filename, $rest);
return ("<font color=red>BOM found, automatically removed._<a href=http://www.joyphper.net>http://www.joyphper.net</a></font>");
} else {
return ("<font color=red>BOM found.</font>");
}
}
else return ("BOM Not Found.");
}
function rewrite ($filename, $data) {
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>
復(fù)制代碼 代碼如下:
<?php
//此文件用于快速測(cè)試UTF8編碼的文件是不是加了BOM,并可自動(dòng)移除
$basedir="."; //修改此行為需要檢測(cè)的目錄,點(diǎn)表示當(dāng)前目錄
$auto=1; //是否自動(dòng)移除發(fā)現(xiàn)的BOM信息。1為是,0為否。
//以下不用改動(dòng)
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) {
global $auto;
$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) {
if ($auto==1) {
$rest=substr($contents, 3);
rewrite ($filename, $rest);
return ("<font color=red>BOM found, automatically removed.</font>");
} else {
return ("<font color=red>BOM found.</font>");
}
}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的代碼
復(fù)制代碼 代碼如下:
<?php
if (isset($_GET['dir'])){ //設(shè)置文件目錄
$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")." <br>";
}else{
$dirname = $basedir."/".$file;
checkdir($dirname);
}
}
}
closedir($dh);
}
}
function checkBOM ($filename) {
global $auto;
$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) {
if ($auto == 1) {
$rest = substr($contents, 3);
rewrite ($filename, $rest);
return ("<font color=red>BOM found, automatically removed._<a href=http://www.joyphper.net>http://www.joyphper.net</a></font>");
} else {
return ("<font color=red>BOM found.</font>");
}
}
else return ("BOM Not Found.");
}
function rewrite ($filename, $data) {
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>
您可能感興趣的文章:
- php實(shí)現(xiàn)批量修改文件名稱的方法
- PHP chmod 函數(shù)與批量修改文件目錄權(quán)限
- PHP批量檢測(cè)并去除文件BOM頭代碼實(shí)例
- 如何使用PHP批量去除文件UTF8 BOM信息
- PHP批量刪除、清除UTF-8文件BOM頭的代碼實(shí)例
- php中數(shù)據(jù)的批量導(dǎo)入(csv文件)
- php實(shí)現(xiàn)批量下載百度云盤文件例子分享
- php實(shí)現(xiàn)批量壓縮圖片文件大小的腳本
- php 批量生成html,txt文件的實(shí)現(xiàn)代碼
- php中批量替換文件名的實(shí)現(xiàn)代碼
- php中批量修改文件后綴名的函數(shù)代碼
- php實(shí)現(xiàn)文件編碼批量轉(zhuǎn)換
- PHP批量修改文件名稱的方法分析
相關(guān)文章
PHP產(chǎn)生不重復(fù)隨機(jī)數(shù)的5個(gè)方法總結(jié)
這篇文章主要介紹了PHP產(chǎn)生不重復(fù)隨機(jī)數(shù)的5個(gè)方法總結(jié),PHP隨機(jī)數(shù)經(jīng)常在項(xiàng)目中使用,本文總結(jié)了網(wǎng)絡(luò)上的和自己項(xiàng)目中用到的隨機(jī)數(shù)生成方法,需要的朋友可以參考下2014-11-11php 網(wǎng)頁游戲開發(fā)入門教程一(webgame+design)
網(wǎng)頁游戲開發(fā)入門教程一 webgame+design , 大家可以參考下。2009-10-10Laravel (Lumen) 解決JWT-Auth刷新token的問題
今天小編就為大家分享一篇Laravel (Lumen) 解決JWT-Auth刷新token的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10php實(shí)現(xiàn)的太平洋時(shí)間和北京時(shí)間互轉(zhuǎn)的自定義函數(shù)分享
這篇文章主要介紹了php實(shí)現(xiàn)的太平洋時(shí)間和北京時(shí)間互轉(zhuǎn)的自定義函數(shù)分享,主要靠date_default_timezone_set函數(shù)來實(shí)現(xiàn),需要的朋友可以參考下2014-08-08Yii2創(chuàng)建控制器(createController)方法詳解
這篇文章主要介紹了Yii2創(chuàng)建控制器(createController)的方法,結(jié)合實(shí)例形式分析了Yii創(chuàng)建控制器所使用到的方法、操作步驟與相關(guān)技巧,需要的朋友可以參考下2016-07-07Yii使用DbTarget實(shí)現(xiàn)日志功能的示例代碼
這篇文章主要介紹了Yii使用DbTarget實(shí)現(xiàn)日志功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07