欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP去除BOM文档

程序员文章站 2024-01-26 21:53:52
...
  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
相关标签: PHP去除BOM文档