2012-05-27 38 views
2

我有這個字符串:試圖從字符串(文件/文件夾結構)創建陣列

css/ 
reset.css 
style.css 
img/ 
js/ 
lib/ 
    jquery.js 
script.js 
index.php 

,我想這個數組:

Array 
(
    [js/] => Array 
     (
      [lib/] => Array 
       (
        [jquery.js] => 
       ) 
      [script.js] => 
     ) 
    [index.php] => 
) 

我寫了這個代碼:

class Structurer{ 
    public function parse(){ 
     $output = array(); 
     $level = 0; 
     $currentFolder = ''; 

     function throughtLevels(&$arr, $key, $newKey, $newValue){ 
      if(is_array($arr)){ 
       foreach($arr as $k => $v){ 
        if($k == $key){echo 'ok'; 
         if(is_array($arr[$k])) $arr[$k] = array_merge($arr[$k], array($newKey => $newValue)); 
         else $arr[$k] = array($newKey => $newValue); 
        } 
        else throughtLevels($arr[$k], $key, $newKey, $newValue); 
       } 
      } 
     } // throughtLevels 
     while($this->moveToNextLine()){ 
      $curl = ''; 

      if(preg_match_all('#\t#', $this->currentLine, $n)){ 
       $this->currentLine = str_replace("\t", '', $this->currentLine); 
       $level = count($n[0]); 
      } 
      else $level = 0; 

      if(preg_match_all('#\[(.*)\]#', $this->currentLine, $com)){ 
       $this->currentLine = str_replace($com[0], '', $this->currentLine); 
       $curl = implode('', $com[1]); 
      } 

      if(preg_match('#\/#', $this->currentLine)){ 
       if($level > 0) throughtLevels($output, $currentFolder, $this->currentLine, array()); 
       else $output[$this->currentLine] = array(); 

       $currentFolder = $this->currentLine; 
      } else { 
       if(!empty($this->currentLine)){ 
        if($level > 0)throughtLevels($output, $currentFolder, $this->currentLine, $curl); 
        else $output[$this->currentLine] = $curl; 
       } 
      } 
     } 

     echo '<pre>' . print_r($output, 1) . '</pre>'; 
    } // parse(value) 

    private function moveToNextLine(){ 
     if($this->currentLineNum >= count($this->lines) -1) return false; 
     $this->currentLine = $this->lines[++$this->currentLineNum]; 
     return true; 
    } // moveTonextLine 


    private static function cleanup($value){ 
     $value = str_replace(array("\r\n", "\r"), "\n", $value); 
     $value = preg_replace('#/\*[^*]*\*+([^/][^*]*\*+)*/#', '', $value); 
     $value = preg_replace('#^\n#', '', $value); 

     return $value; 
    } // cleanup(value) 


    public function __construct($value){ 
     $this->base = dirname(__FILE__); 
     $this->currentLineNb = -1; 
     $this->currentLine = ''; 
     $this->lines = explode("\n", self::cleanup($value)); 

     $this->parse(); 
    } // __construct(value) 


    private $lines = array(); 
    private $currentLineNum = -1; 
    private $currentLine = ''; 
} // Structurer 
$input = <<<EOT 
css/ 
    reset.css 
    style.css 
img/ 
js/ 
    lib/ 
     jquery.js 
    script.js 
index.php 
EOT; 
$arr = new Structurer($input); 

它產生的數組:

Array 
(
    [js/] => Array 
     (
      [lib/] => Array 
       (
        [jquery.js] => 
        [script.js] => 
       ) 
     ) 
    [index.php] => 
) 

我真的不知道如何解決這個問題。我想我不遠,但我不能前進。

+0

你能告訴更多關於爲什麼你在格式的字符串?它是否是您控制範圍之外的另一個流程的輸出?另外,你會用這種輸出做什麼? –

回答

1

我建議你看一看RecursiveDirectoryIterator

線性迭代,使用方法:

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $entry) { 
    // do stuff with $entry 
} 
+1

他沒有迭代目錄,而是遍歷包含目錄結構的字符串。 – Jeroen

+0

你好傑克,謝謝你的回答。我看到RecursiveDirectoryIterator使用真正的目錄或我帶有一個普通的字符串...但我會檢查這個類! :-) –

+0

@Jeroen你是對的,我的反應太快了;-) –

相關問題