閱讀616 返回首頁    go 阿裏雲 go 技術社區[雲棲]


清理php中的bom

  1. <?php
  2. if (isset($_GET['dir'])){ //config the basedir
  3. $basedir=$_GET['dir'];
  4. }else{
  5. $basedir = '.';
  6. }
  7. $auto = 1;
  8. checkdir($basedir);
  9. function checkdir($basedir){
  10. if ($dh = opendir($basedir)) {
  11. while (($file = readdir($dh)) !== false) {
  12. if ($file != '.' && $file != '..'){
  13. if (!is_dir($basedir."/".$file)) {
  14. echo "filename: $basedir/$file ".checkBOM("$basedir/$file")."
  15. ";
  16. }else{
  17. $dirname = $basedir."/".$file;
  18. checkdir($dirname);
  19. }
  20. }
  21. }
  22. closedir($dh);
  23. }
  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 && ord($charset[3]) == 191) {
  32. if ($auto == 1) {
  33. $rest = substr($contents, 3);
  34. rewrite ($filename, $rest);
  35. return ("BOM found, automatically removed.");
  36. } else {
  37. return ("BOM found.");
  38. }
  39. }
  40. else return ("BOM Not Found.");
  41. }
  42. function rewrite ($filename, $data) {
  43. $filenum = fopen($filename, "w");
  44. flock($filenum, LOCK_EX);
  45. fwrite($filenum, $data);
  46. fclose($filenum);
  47. }
  48. ?>

最後更新:2017-04-03 12:55:19

  上一篇:go Shallow Copy and Deep Copy
  下一篇:go 麵向對象的基本設計原則