Home  >  Article  >  Backend Development  >  PHP remove BOM document

PHP remove BOM document

WBOY
WBOYOriginal
2016-07-25 08:43:41807browse
  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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn