首页 >后端开发 >php教程 >PHP去除BOM文档

PHP去除BOM文档

WBOY
WBOY原创
2016-07-25 08:43:41834浏览
  1. //remove the utf-8 boms
  2. //by magicbug at gmail dot com
  3. if (isset($_GET['dir'])){ //要去除的文件目录,无参数则为文件当前目录。
  4. $basedir=$_GET['dir'];
  5. }else{
  6. $basedir = '.';
  7. }
  8. $auto = 1;
  9. checkdir($basedir);
  10. function checkdir($basedir){
  11. if ($dh = opendir($basedir)) {
  12. while (($file = readdir($dh)) !== false) {
  13. if ($file != '.' && $file != '..'){
  14. if (!is_dir($basedir."/".$file)) {
  15. echo "filename: $basedir/$file ".checkBOM("$basedir/$file")."
    ";
  16. }else{
  17. $dirname = $basedir."/".
  18. $file;
  19. checkdir($dirname);
  20. }
  21. }
  22. }
  23. closedir($dh);
  24. }
  25. function checkBOM ($filename) {
  26. global $auto;
  27. $contents = file_get_contents($filename);
  28. $charset[1] = substr($contents, 0, 1);
  29. $charset[2] = substr($contents, 1, 1);
  30. $charset[3] = substr($contents, 2, 1);
  31. if (ord($charset[1]) == 239 && ord($charset[2]) == 187 &&
  32. ord($charset[3]) == 191) {
  33. if ($auto == 1) {
  34. $rest = substr($contents, 3);
  35. rewrite ($filename, $rest);
  36. return ("BOM found,automatically removed.");
  37. } else {
  38. return ("BOM found.");
  39. }
  40. }
  41. else return ("BOM Not Found.");
  42. }
  43. function rewrite ($filename, $data) {
  44. $filenum = fopen($filename, "w");
  45. flock($filenum, LOCK_EX);
  46. fwrite($filenum, $data);
  47. fclose($filenum);
  48. }
  49. ?>
复制代码

PHP, BOM


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn