批量去除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
//此文件用于快速測試UTF8編碼的文件是不是加了BOM,并可自動(dòng)移除
$basedir="."; //修改此行為需要檢測的目錄,點(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
//此文件用于快速測試UTF8編碼的文件是不是加了BOM,并可自動(dòng)移除
$basedir="."; //修改此行為需要檢測的目錄,點(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批量檢測并去除文件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)文章
Yii數(shù)據(jù)模型中rules類驗(yàn)證器用法分析
這篇文章主要介紹了Yii數(shù)據(jù)模型中rules類驗(yàn)證器用法,結(jié)合實(shí)例形式分析了rules類驗(yàn)證器的簡單使用方法,代碼注釋中包含了相關(guān)方法的使用說明,需要的朋友可以參考下2016-07-07
laravel 框架實(shí)現(xiàn)無限級(jí)分類的方法示例
這篇文章主要介紹了laravel 框架實(shí)現(xiàn)無限級(jí)分類的方法,結(jié)合實(shí)例形式分析了Laravel框架基于遞歸遍歷實(shí)現(xiàn)無限極分類輸出的相關(guān)控制器及視圖操作技巧,需要的朋友可以參考下2019-10-10
在swoole中制作一款仿制laravel的框架的實(shí)例代碼
這篇文章主要介紹了在swoole中制作一款仿制laravel的框架,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
php用戶注冊頁面利用js進(jìn)行表單驗(yàn)證具體實(shí)例
這篇文章介紹了php用戶注冊頁面利用js進(jìn)行表單驗(yàn)證具體實(shí)例,有需要的朋友可以參考一下2013-10-10
PHP實(shí)現(xiàn)微信申請(qǐng)退款流程實(shí)例代碼
本篇文章給大家介紹php實(shí)現(xiàn)微信申請(qǐng)退款流程,使用到官方提供的SDK中的最重要的一個(gè)類文件WxPay.Api.php中提供的refund()方法來實(shí)現(xiàn)的,完整大家大家參考下本文2018-03-03

