PHP实例:检测并清除文件开头的BOM信息

php中文网
发布: 2016-07-25 08:51:28
原创
1152人浏览过
  1. ini_set('memory_limit', '-1');
  2. /*检测并清除BOM*/
  3. $basedir = dirname(__FILE__);//扫描当前文件路径 可自动设置
  4. $auto = 1;
  5. checkdir($basedir);
  6. function checkdir($basedir){
  7. if($dh = opendir($basedir)){
  8. while(($file = readdir($dh)) !== false){
  9. if($file != '.' && $file != '..'){
  10. if(!is_dir($basedir."/".$file)){
  11. echo "filename: $basedir/$file ".checkBOM("$basedir/$file")."
    ";
  12. }else{
  13. $dirname = $basedir."/".$file;
  14. checkdir($dirname);
  15. }
  16. }
  17. }//end while
  18. closedir($dh);
  19. }//end if($dh
  20. }//end function
  21. function checkBOM($filename){
  22. global $auto; // bbs.it-home.org
  23. $contents = file_get_contents($filename);
  24. $charset[1] = substr($contents, 0, 1);
  25. $charset[2] = substr($contents, 1, 1);
  26. $charset[3] = substr($contents, 2, 1);
  27. if(ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191){
  28. if($auto == 1){
  29. $rest = substr($contents, 3);
  30. rewrite ($filename, $rest);
  31. return "BOM found, automatically removed.";
  32. }else{
  33. return ("BOM found.");
  34. }
  35. }
  36. else return ("BOM Not Found.");
  37. }//end function
  38. function rewrite($filename, $data){
  39. $filenum = fopen($filename, "w");
  40. flock($filenum, LOCK_EX);
  41. fwrite($filenum, $data);
  42. fclose($filenum);
  43. }//end function
复制代码


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号