当前位置:Gxlcms > php框架 > 批量去除PHP文件中bom的PHP代码

批量去除PHP文件中bom的PHP代码

时间:2021-07-01 10:21:17 帮助过:54人阅读

需要去除BOM,就把附件里的tool.php文件放到目标目录,然后在浏览器访问tool.php即可!
代码如下:
  1. <br><?php <br>//此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除 <br>$basedir="."; //修改此行为需要检测的目录,点表示当前目录 <br>$auto=1; //是否自动移除发现的BOM信息。1为是,0为否。 <br>//以下不用改动 <br>if ($dh = opendir($basedir)) { <br>while (($file = readdir($dh)) !== false) { <br>if ($file!='.' && $file!='..' && !is_dir($basedir."/".$file)) <br>echo "filename: $file ".checkBOM("$basedir/$file")." <br>"; <br>} <br>closedir($dh); <br>} <br>function checkBOM ($filename) { <br>global $auto; <br>$contents=file_get_contents($filename); <br>$charset[1]=substr($contents, 0, 1); <br>$charset[2]=substr($contents, 1, 1); <br>$charset[3]=substr($contents, 2, 1); <br>if (ord($charset[1])==239 && ord($charset[2])==187 && ord($charset[3])==191) { <br>if ($auto==1) { <br>$rest=substr($contents, 3); <br>rewrite ($filename, $rest); <br>return ("<font color=red>BOM found, automatically removed.</font>"); <br>} else { <br>return ("<font color=red>BOM found.</font>"); <br>} <br>}else <br>return ("BOM Not Found."); <br>} <br>function rewrite ($filename, $data) { <br>$filenum=fopen($filename,"w"); <br>flock($filenum,LOCK_EX); <br>fwrite($filenum,$data); <br>fclose($filenum); <br>} <br>?> <br> <br>PHP批量去除PHP文件中bom的代码 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><?php <br>if (isset($_GET['dir'])){ //设置文件目录 <br>$basedir=$_GET['dir']; <br>}else{ <br>$basedir = '.'; <br>} <br>$auto = 1; <br>checkdir($basedir); <br>function checkdir($basedir){ <br>if ($dh = opendir($basedir)) { <br>while (($file = readdir($dh)) !== false) { <br>if ($file != '.' && $file != '..'){ <br>if (!is_dir($basedir."/".$file)) { <br>echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>"; <br>}else{ <br>$dirname = $basedir."/".$file; <br>checkdir($dirname); <br>} <br>} <br>} <br>closedir($dh); <br>} <br>} <br>function checkBOM ($filename) { <br>global $auto; <br>$contents = file_get_contents($filename); <br>$charset[1] = substr($contents, 0, 1); <br>$charset[2] = substr($contents, 1, 1); <br>$charset[3] = substr($contents, 2, 1); <br>if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) { <br>if ($auto == 1) { <br>$rest = substr($contents, 3); <br>rewrite ($filename, $rest); <br>return ("<font color=red>BOM found, automatically removed._<a href=http://www.joyphper.net>http://www.joyphper.net</a></font>"); <br>} else { <br>return ("<font color=red>BOM found.</font>"); <br>} <br>} <br>else return ("BOM Not Found."); <br>} <br>function rewrite ($filename, $data) { <br>$filenum = fopen($filename, "w"); <br>flock($filenum, LOCK_EX); <br>fwrite($filenum, $data); <br>fclose($filenum); <br>} <br>?> <br></li><li> </li><li> </li></ol></pre>

人气教程排行