Home > Backend Development > PHP Tutorial > Detect whether a file has a bom header - PHP

Detect whether a file has a bom header - PHP

WBOY
Release: 2016-07-25 08:49:53
Original
995 people have browsed it
Detect whether a file has a bom header - PHP
  1. //This file is used to quickly test whether the UTF8 encoded file has a BOM added and can be automatically removed
  2. //By Bob Shen
  3. $basedir="."; //Modify this The directory whose behavior needs to be detected, dot indicates the current directory
  4. $auto=1; //Whether to automatically remove the discovered BOM information. 1 means yes, 0 means no.
  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)) echo "filename: $file ".checkBOM("$basedir/$file")."
    ";
  9. }
  10. closedir($dh);
  11. }
  12. function checkBOM ($filename) {
  13. global $auto;
  14. $contents=file_get_contents($filename);
  15. $charset[1]=substr($contents, 0, 1 );
  16. $charset[2]=substr($contents, 1, 1);
  17. $charset[3]=substr($contents, 2, 1);
  18. if (ord($charset[1])==239 && ord($charset[2])==187 && ord($charset[3])==191) {
  19. if ($auto==1) {
  20. $rest=substr($contents, 3);
  21. rewrite ($filename, $rest);
  22. return ("BOM found, automatically removed.");
  23. } else {
  24. return ("BOM found .");
  25. }
  26. }
  27. else return ("BOM Not Found.");
  28. }
  29. function rewrite ($filename, $data) {
  30. $filenum=fopen($filename,"w ");
  31. flock($filenum,LOCK_EX);
  32. fwrite($filenum,$data);
  33. fclose($filenum);
  34. }
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template