PHP網(wǎng)站建設(shè)之去除文件bom頭信息

2023-03-06    分類: 網(wǎng)站建設(shè)

BOM是用來判斷文本文件是哪一種Unicode編碼的標(biāo)記,其本身是一個(gè)Unicode字符("\uFEFF"),位于文本文件頭部,BOM本來不影響代碼的解析,但是php除外,PHP會(huì)解析BOM,會(huì)輸出在頁面里,造成前端有占位發(fā)生布局位移,如果不了解的php BOM 就會(huì)對(duì)這個(gè)平白多出來的東西感到莫名其妙。
我們?nèi)サ艟W(wǎng)站php文件里的BOM信息呢
編輯器可以無保存bom文件,utf-8和utf-8 with bom ,要保存了bom文件 php會(huì)報(bào)錯(cuò)namespace 必須是第一行的代碼,現(xiàn)在是bom信息是第一行,雖然你看不到但是他就是在第一行

網(wǎng)站制作1

我們怎么樣通過代碼的形式批量的去掉文件的bom信息
在網(wǎng)站根目錄下新建一個(gè)nobomb.php的文件,文件的代碼為以下內(nèi)容。主要解決模板多出空的內(nèi)容、驗(yàn)證碼不顯示等問題。然后在瀏覽器直接訪問運(yùn)行nobom.php文件即可。
<?php
if(isset($_GET['dir'])){
$basedir=$_GET['dir'];
}else{
$basedir='.';
}
$auto=1;
checkdir($basedir);
functioncheckdir($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);
}
}
functioncheckBOM($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("<fontcolor=red>BOMfound,automaticallyremoved.</font>");
}else{
return("<fontcolor=red>BOMfound.</font>");
}
}elsereturn("BOMNotFound.");
}
functionrewrite($filename,$data){
$filenum=fopen($filename,"w");
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}

網(wǎng)站制作2

運(yùn)行上面的代碼就可以清除文件的bom信息

網(wǎng)站制作3

上面的截圖看是沒有發(fā)現(xiàn)帶有bom的文件的,有bom的文件 會(huì)出現(xiàn)字體加紅色。
去掉后訪問網(wǎng)站訪問正常

網(wǎng)站制作4

網(wǎng)頁題目:PHP網(wǎng)站建設(shè)之去除文件bom頭信息
文章鏈接:http://www.muchs.cn/news37/241887.html

網(wǎng)站建設(shè)、網(wǎng)絡(luò)推廣公司-創(chuàng)新互聯(lián),是專注品牌與效果的網(wǎng)站制作,網(wǎng)絡(luò)營銷seo公司;服務(wù)項(xiàng)目有網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)