2

I have this String :

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

and I want that Array :

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

I wrote this code :

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);

and it produce that Array :

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

I really don't know how to fix this. I think I'm not far but I can't go forward.

1
  • Could you tell more about why you have a string in that format? Is it the output of another process that's outside of your control? Also, what would you do with that kind of output? Commented May 27, 2012 at 13:29

1 Answer 1

0

I suggest you take a look at RecursiveDirectoryIterator

To iterate linearly, use:

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $entry) {
    // do stuff with $entry
}
Sign up to request clarification or add additional context in comments.

2 Comments

He's not iterating over a directory but over a string containing a directory structure.
Hello Jack and thank you for your answer. I see RecursiveDirectoryIterator use truly Directory or I came with a regular String... but I will check this class! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.