+ -
当前位置:首页 → 问答吧 → 递归删除utf8文件的bom头(该bom头可能导致php产生意外输出)

递归删除utf8文件的bom头(该bom头可能导致php产生意外输出)

时间:2010-09-02

来源:互联网

复制代码
  1. <?php
  2. //author: selfimpr
  3. //blog: http://blog.csdn.net/lgg201
  4. //mail: [email protected]
  5. //EF BB BF这三个字节称为bom头
  6. function hasbom(&$content) {
  7.     $firstline = $content[0];
  8.     return ord(substr($firstline, 0, 1)) === 0xEF
  9.         and ord(substr($firstline, 1, 1)) === 0xBB
  10.         and ord(substr($firstline, 2, 1)) === 0xBF;
  11. }
  12. function unsetbom(&$content) {
  13.     hasbom($content) and ($content[0] = substr($content[0], 3));
  14. }
  15. function write($filename, &$content) {
  16.     $file = fopen($filename, 'w');
  17.     fwrite($file, implode($content, ''));
  18.     fclose($file);
  19. }
  20. function filenames($path) {
  21.     $directory = opendir($path);
  22.     while (false != ($filename = readdir($directory))) strpos($filename, '.') !== 0 and $filenames[] = $filename;
  23.     closedir($directory);
  24.     return $filenames;
  25. }
  26. function process($path) {
  27.     $parent = opendir($path);
  28.     while (false != ($filename = readdir($parent))) {
  29.         echo $filename."\n";
  30.         if(strpos($filename, '.') === 0) continue;
  31.         if(is_dir($path.'/'.$filename)) {
  32.             process($path.'/'.$filename);
  33.         } else {
  34.             $content = file($path.'/'.$filename);
  35.             unsetbom($content);
  36.             write($path.'/'.$filename, $content);
  37.         }
  38.     }
  39.     closedir($parent);
  40. }
  41. process('/home/selfimpr/t');
  42. ?>

作者: selfimpr   发布时间: 2010-09-02

  -,- 这也行?

作者: ★star★   发布时间: 2010-09-03