国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP批量刪除、清除UTF-8文件BOM頭的代碼實例

2020-03-22 19:11:11
字體:
來源:轉載
供稿:網友
記得運行代碼前先把文件備份一下哦,避免出現失敗問題。

代碼一:
復制代碼 代碼如下:
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.");
}
代碼二:
復制代碼 代碼如下:
?php
header('content-Type: text/html; charset=utf-8');
if(isset($_GET['dir'])){ //設置文件目錄,如果沒有設置,則自動設置為當前文件所在目錄
$basedir=$_GET['dir'];
}else{
$basedir='.';
}
$auto=1;/*設置為1標示檢測BOM并去除,設置為0標示只進行BOM檢測,不去除*/echo '當前查找的目錄為:'.$basedir.'當前的設置是:';
echo $auto?'檢測文件BOM同時去除檢測到BOM文件的BOM br / ':'只檢測文件BOM不執行去除BOM操作 br / checkdir($basedir);
function checkdir($basedir){
if($dh=opendir($basedir)){
while (($file=readdir($dh)) !== false){
if($file != '.' && $file != '..'){
if(!is_dir($basedir.'/'.$file)){
echo '文件: '.$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并已自動去除 /font
}else{
return (' font color=red 找到BOM /font
}
}else{
return (' 沒有找到BOM');
}
}
function rewrite($filename,$data){
$filenum=fopen($filename,'w');
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
?
代碼三:
復制代碼 代碼如下:
##把該文件放在需求去除BOM頭的目錄下跑一下卻可。
?php
if (isset ( $_GET ['dir'] )) { // config the basedir
$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) { // BOM
// 的前三個字符的ASCII
// 碼分別為
// 239
// 187
// 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 );
}
?

二、Python
復制代碼 代碼如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*-import osdef delBOM():
file_count = 0
bom_files = []for dirpath, dirnames, filenames in os.walk('.'):
if(len(filenames)):
for filename in filenames:
file_count += 1
file = open(dirpath + "/" + filename, 'r+')
file_contents = file.read()if(len(file_contents) 3):
if(ord(file_contents[0]) == 239 and ord(file_contents[1]) == 187 and ord(file_contents[2]) == 191):
bom_files.append(dirpath + "/" + filename)
file.seek(0)
file.write(file_contents[3:])
print bom_files[-1], "BOM found. Deleted."
file.close()print file_count, "file(s) found.", len(bom_files), "file(s) have a bom. Deleted."if __name__ == "__main__":
delBOM()
PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙岩市| 华池县| 临潭县| 万全县| 静乐县| 丘北县| 洛南县| 竹山县| 永和县| 宁河县| 望谟县| 彩票| 张家界市| 奉化市| 关岭| 呼伦贝尔市| 溆浦县| 平乐县| 英吉沙县| 中西区| 白水县| 丽江市| 兖州市| 开江县| 亳州市| 开平市| 马鞍山市| 邢台县| 昭平县| 沁源县| 巴南区| 吉木乃县| 进贤县| 漠河县| 磐安县| 罗田县| 木兰县| 武定县| 教育| 莱芜市| 云安县|