服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > php教程 >

php去bom方法,去除PHP文件中UTF8的BOM

时间:2011-10-15 10:49来源:未知 作者:最模板 点击:
将以下代码保存在网站根目录下,后缀为.php,然后运行即可去除网站路径下所有php文件的bom。 ?php //removetheutf-8boms //byzuimoban.com if (isset( $_GET [ 'dir' ])){ //configthebasedir $basedir = $_GET [ 'dir' ];

 将以下代码保存在网站根目录下,后缀为.php,然后运行即可去除网站路径下所有php文件的bom。

  1. <?php 
  2.  
  3. //remove the utf-8 boms 
  4. //by zuimoban.com 
  5.  
  6. if (isset($_GET['dir'])){ //config the basedir 
  7. $basedir=$_GET['dir']; 
  8. }else
  9. $basedir = '.'
  10.  
  11. $auto = 1; 
  12.  
  13. checkdir($basedir); 
  14.  
  15. function checkdir($basedir){ 
  16. if ($dh = opendir($basedir)) { 
  17. while (($file = readdir($dh)) !== false) { 
  18. if ($file != '.' && $file != '..'){ 
  19. if (!is_dir($basedir."/".$file)) { 
  20. echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." 
  21. "; 
  22. }else
  23. $dirname = $basedir."/".$file
  24. checkdir($dirname); 
  25. closedir($dh); 
  26.  
  27. function checkBOM ($filename) { 
  28. global $auto
  29. $contents = file_get_contents($filename); 
  30. $charset[1] = substr($contents, 0, 1); 
  31. $charset[2] = substr($contents, 1, 1); 
  32. $charset[3] = substr($contents, 2, 1); 
  33. if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) { 
  34. if ($auto == 1) { 
  35. $rest = substr($contents, 3); 
  36. rewrite ($filename$rest); 
  37. return ("BOM found, automatically removed."); 
  38. else { 
  39. return ("BOM found."); 
  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. ?> 

 

(责任编辑:最模板)
顶一下
(5)
100%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容