window 開發時候使用記事本打開代碼文件,記事本會在文件加上bom頭 (字節順序標記,傳輸標識)php
使用 Nodepad++ 能夠單個修改文件編碼html
使用框架的時候涉及的文件衆多,可使用代碼批量去除dom 頭 。已下PHP代碼親測可用程序員
<?php header('content-Type: text/html; charset=utf-8'); $auto=1;/*設置爲1標示檢測BOM並去除,設置爲0標示只進行BOM檢測,不去除*/ $basedir='.'; $loop=true;//www.phpernote.com echo '當前查找的目錄爲:'.$basedir.'當前的設置是:'; echo '(1)',$loop?'檢查當前目錄以及當前目錄的子目錄':'只針對當前目錄進行檢測'; echo '(2)',$auto?'檢測文件BOM同時去除檢測到BOM文件的BOM<br />':'只檢測文件BOM不執行去除BOM操做<br />'; checkdir($basedir,$loop); function checkdir($basedir='',$loop=true){ $basedir=empty($basedir)?'.':$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{ if(!$loop) continue; $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); }
代碼轉載自:框架
文章地址 dom
做者站點oop
php程序員的筆記 編碼