Heim  >  Artikel  >  Backend-Entwicklung  >  检测文件是否有bom头 - PHP

检测文件是否有bom头 - PHP

WBOY
WBOYOriginal
2016-07-25 08:49:53924Durchsuche
检测文件是否有bom头 - PHP
  1. //此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除
  2. //By Bob Shen
  3. $basedir="."; //修改此行为需要检测的目录,点表示当前目录
  4. $auto=1; //是否自动移除发现的BOM信息。1为是,0为否。
  5. //以下不用改动
  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. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:随时发消息给自己的微信 - PHP Nächster Artikel:排序