私はこの文字列を持っています:
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 を生成します:
Array
(
[js/] => Array
(
[lib/] => Array
(
[jquery.js] =>
[script.js] =>
)
)
[index.php] =>
)
これを修正する方法が本当にわかりません。遠くないと思いますが、前に進むことはできません。