php将大括号插入数组

php将大括号插入数组,php,brackets,Php,Brackets,我想检查一个打开的.txt文件中是否有大括号可以打开和关闭,如下所示: file { nextopen { //content } } 不,这不是我自己的语言或任何东西,但我想得到,比如nextopen函数和括号内的所有内容,以及file函数内的所有内容,并将其添加到数组中,如果你知道我的意思的话。因此大括号内的所有内容都将在一个数组中。如果你知道怎么做,请回复 数组应如下所示: array( [file] => '{ nextopen { //content } }', [next

我想检查一个打开的.txt文件中是否有大括号可以打开和关闭,如下所示:

file {

nextopen {
//content
}

}
不,这不是我自己的语言或任何东西,但我想得到,比如nextopen函数和括号内的所有内容,以及file函数内的所有内容,并将其添加到数组中,如果你知道我的意思的话。因此大括号内的所有内容都将在一个数组中。如果你知道怎么做,请回复

数组应如下所示:

array(
[file] => '{ nextopen { //content } }',
[nextopen] => '{ //content }'
);

基本算法如下所示

  • 对于每个序列{此处无大括号},将其放入缓冲区,并替换为一个幻数,标识其在缓冲区中的位置
  • 重复(1),直到找不到更多的序列
  • 对于缓冲区中的每个条目-如果它包含幻数,则用缓冲区中相应的字符串替换每个数字
  • 缓冲区就是我们要找的
  • 在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;
        }
    
    
    
    }
    
    试验

    结果

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

    如果您有任何问题,请告诉我。

    这方面的基本算法如下所示

  • 对于每个序列{此处无大括号},将其放入缓冲区,并替换为一个幻数,标识其在缓冲区中的位置
  • 重复(1),直到找不到更多的序列
  • 对于缓冲区中的每个条目-如果它包含幻数,则用缓冲区中相应的字符串替换每个数字
  • 缓冲区就是我们要找的
  • 在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;
        }
    
    
    
    }
    
    试验

    结果

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

    如果您有任何问题,请告诉我。

    不,我不知道您的确切意思。你能举个例子说明结果数组应该是什么样的吗?增加了数组应该是什么样的这可能并不像人们直观地认为的那么简单。您需要对输入进行标记化。为此,您需要提供更多关于输入语法的输入。例如;标识符的标准是什么?(换行符、空格、字符等)。请参阅维基百科关于词法分析的文章,了解为什么它不那么琐碎:不,我不知道你的确切意思。你能举个例子说明结果数组应该是什么样的吗?增加了数组应该是什么样的这可能并不像人们直观地认为的那么简单。您需要对输入进行标记化。为此,您需要提供更多关于输入语法的输入。例如;标识符的标准是什么?(换行符、空格、字符等)。请参阅维基百科关于词法分析的文章,了解词法分析并非如此琐碎的原因: