Bunun için temel algoritmalar aşağıdaki gibidir
- Her dizisi için
{ no-braces-here }, bir tampon koymak ve bir sihirli sayı tampon konumunu belirleme ile değiştirin
- tekrar (1) kadar artık dizileri bulunabilir
- bir tampon her giriş için - bu sihirli sayılar içeriyorsa, tampon karşılık gelen dize ile her numarayı değiştirin.
- tampon biz aradığınızı
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);
sonuç
Array
(
[0] => { baz }
[1] => { quux }
[2] => { bar and { baz } and { quux } }
[3] => { foo and { bar and { baz } and { quux } } hello! }
)
Herhangi bir sorunuz varsa bana bildirin.