0

I would like to check a opened .txt file for braces that open and close like below:

file {

nextopen {
//content
}

}

no this is not my own language or anything, but I want to get say the nextopen function and all the contents inside the brackets, and all the stuff from inside the file function and add it to an array if you know what i mean. so all the content inside the braces will be in an array. if you know how to do this please reply.

array should look like this:

array(
[file] => '{ nextopen { //content } }',
[nextopen] => '{ //content }'
);
3
  • No, I don't know what you mean exactly. Can you give an example of how the resulting array should look like? Commented Apr 16, 2010 at 3:41
  • added what array should look like Commented Apr 16, 2010 at 3:44
  • 1
    This is probably not gonna be as trivial as one might intuitively think. You would need to tokenize the input. For this, you need to give more input about the grammar of the input. For instance; what are the criteria for the identifiers? (newlines, spaces, characters, etc.). See the Wikipedia article on Lexical analysis as to why it is not so trivial: en.wikipedia.org/wiki/Lexical_analysis Commented Apr 16, 2010 at 3:55

1 Answer 1

3

The basic algo for this is like the following

  1. for every sequence { no-braces-here }, put it in a buffer and replace with a magic number identifying its position in the buffer
  2. repeat (1) until no more sequences can be found
  3. for every entry in a buffer - if it contains magic numbers, replace each number with the corresponding string from the buffer.
  4. the buffer is what we're looking for

in php

class Parser
{
    var $buf = array();

    function put_to_buf($x) {
        $this->buf[] = $x[0];
        return '@' . (count($this->buf) - 1) . '@';
    }

    function get_from_buf($x) {
        return $this->buf[intval($x[1])];
    }

    function replace_all($re, $str, $callback) {
        while(preg_match($re, $str))
            $str = preg_replace_callback($re, array($this, $callback), $str);
        return $str;
    }

    function run($text) {
        $this->replace_all('~{[^{}]*}~', $text, 'put_to_buf');
        foreach($this->buf as &$s)
            $s = $this->replace_all('~@(\d+)@~', $s, 'get_from_buf');
        return $this->buf;
    }



}

test

$p = new Parser;
$a = $p->run("just text { foo and { bar and { baz } and { quux } } hello! } ??");
print_r($a);

result

Array
(
    [0] => { baz }
    [1] => { quux }
    [2] => { bar and { baz } and { quux } }
    [3] => { foo and { bar and { baz } and { quux } } hello! }
)

let me know if you have any questions.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.