基本算法如下
- 对于每个序列
{ no-braces-here }
,将其放入缓冲区并替换为标识其在缓冲区中的位置的幻数
- 重复 (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;
}
}
测试
$p = new Parser;
$a = $p->run("just text { foo and { bar and { baz } and { quux } } hello! } ??");
print_r($a);
结果
Array
(
[0] => { baz }
[1] => { quux }
[2] => { bar and { baz } and { quux } }
[3] => { foo and { bar and { baz } and { quux } } hello! }
)
如果您有任何问题,请告诉我。