php curly braces into array

梦想的初衷 提交于 2019-12-02 12:08:53

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!