Home  >  Article  >  Backend Development  >  How to batch clear BOM in php files

How to batch clear BOM in php files

WBOY
WBOYOriginal
2016-07-25 09:05:19882browse
  1. //This file is used to quickly test whether the UTF8 encoded file has a BOM added and can be automatically removed
  2. $basedir="."; //Modify the directory that needs to be detected for this behavior , the dot indicates the current directory
  3. $auto=1; //Whether to automatically remove the discovered BOM information. 1 means yes, 0 means no.
  4. //link: http://bbs.it-home.org
  5. //No need to change the following
  6. if ($dh = opendir($basedir)) {
  7. while (($file = readdir($dh)) != = false) {
  8. if ($file!='.' && $file!='..' && !is_dir($basedir."/".$file))
  9. echo "filename: $file ".checkBOM(" $basedir/$file")."
    ";
  10. }
  11. closedir($dh);
  12. }
  13. function checkBOM ($filename) {
  14. global $auto;
  15. $contents=file_get_contents($filename);
  16. $charset[1]=substr($contents, 0, 1);
  17. $charset[2]=substr($contents, 1, 1);
  18. $charset[3]=substr($contents, 2, 1);
  19. if (ord($charset[1])==239 && ord($charset[2])==187 && ord($charset[3])==191) {
  20. if ($auto==1) {
  21. $rest=substr($contents, 3);
  22. rewrite ($filename, $rest);
  23. return ("BOM found, automatically removed.");
  24. } else {
  25. return ("BOM found.");
  26. }
  27. }else
  28. return ("BOM Not Found.");
  29. }
  30. function rewrite ($filename, $data ) {
  31. $filenum=fopen($filename,"w");
  32. flock($filenum,LOCK_EX);
  33. fwrite($filenum,$data);
  34. fclose($filenum);
  35. }
  36. ?>
Copy the code

2. Remove BOM codes from PHP files in batches

  1. if (isset($_GET['dir'])){ //Set file directory
  2. $basedir=$_GET['dir'];
  3. }else{
  4. $basedir = '.';
  5. }
  6. $auto = 1;
  7. checkdir($basedir);
  8. function checkdir($basedir){
  9. if ($dh = opendir($basedir)) {
  10. while (($file = readdir($ dh)) !== false) {
  11. if ($file != '.' && $file != '..'){
  12. if (!is_dir($basedir."/".$file)) {
  13. echo "filename: $basedir/$file ".checkBOM("$basedir/$file")."
    ";
  14. }else{
  15. $dirname = $basedir."/".$file;
  16. checkdir($ dirname);
  17. }
  18. }
  19. }
  20. closedir($dh);
  21. }
  22. }
  23. function checkBOM ($filename) {
  24. global $auto;
  25. $contents = file_get_contents($filename);
  26. $charset[1] = substr($contents, 0, 1);
  27. $charset[2] = substr($contents, 1, 1);
  28. $charset[3] = substr($contents, 2, 1);
  29. if (ord($ charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
  30. if ($auto == 1) {
  31. $rest = substr( $contents, 3);
  32. rewrite ($filename, $rest);
  33. return ("BOM found, automatically removed._ http://bbs.it-home.org");
  34. } else {
  35. return ("BOM found.");
  36. }
  37. }
  38. else return ("BOM Not Found.");
  39. }
  40. function rewrite ($filename, $data) {
  41. $filenum = fopen($filename, "w");
  42. flock($filenum, LOCK_EX) ;
  43. fwrite($filenum, $data);
  44. fclose($filenum);
  45. }
  46. ?>
Copy code


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