php遞歸去除bom頭信息

  
  
           
  
  
  1.     因爲本人才疏學淺,對問題認知不免有誤差,本着學習與共享的精神和你們一塊兒探討,如有不對之處,望你們多多批評指正。
  2.  
  3. <?php 
  4. //author:馬榮財 
  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($contentand ($content[0] = substr($content[0], 3)); 
  14.     echo '該文件有bom頭標'
  15. }   
  16. function write($filename, &$content) {   
  17.     $file = fopen($filename'w');   
  18.     fwrite($file, implode($content''));   
  19.     fclose($file);   
  20. }   
  21. function filenames($path) {   
  22.     $directory = opendir($path);   
  23.     while (false != ($filename = readdir($directory))) strpos($filename'.') !== 0 and $filenames[] = $filename;   
  24.     closedir($directory);   
  25.     return $filenames;   
  26. }   
  27. function process($path) {   
  28.     $parent = opendir($path);   
  29.     while (false != ($filename = readdir($parent))) {   
  30.        echo $filename."/n";   
  31.         if(strpos($filename'.') === 0) continue;   
  32.         if(is_dir($path.DIRECTORY_SEPARATOR.$filename)) {   
  33.             process($path.DIRECTORY_SEPARATOR.$filename);   
  34.         } else {   
  35.             $content = file($path.DIRECTORY_SEPARATOR.$filename);   
  36.             unsetbom($content);   
  37.             write($path.DIRECTORY_SEPARATOR .$filename$content);   
  38.         }   
  39.     }   
  40.     closedir($parent);   
  41. }   
  42. process('/tmp/mrc');  
  43. ?> 
相關文章
相關標籤/搜索