• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-25) 9次浏览 已收录 0个评论

需要去除BOM,就把附件里的tool.php文件放到目标目录,然后在浏览器访问tool.php即可!

 <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>


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

 <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($cha<mark>(本文来)源gaodaimacom搞#^代%!码&网(</mark><pre>搞gaodaima代码

rset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
if ($auto == 1) {
$rest = substr($contents, 3);
rewrite ($filename, $rest);
return (“BOM found, automatically removed._http://www.joyphper.net“);
} else {
return (“BOM found.“);
}
}
else return (“BOM Not Found.”);
}
function rewrite ($filename, $data) {
$filenum = fopen($filename, “w”);
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:批量去除PHP文件中bom的PHP代码_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址