我有一个字符串,其中包含几个我想删除的大括号中的句子。这并不难做到(正如我现在所知道的那样。),但真正的麻烦是它是多层次的,我只想去掉顶层括号,让里面的所有东西都完好无损。它看起来像这样:
{Super duper {extra} text.} {Which I'm really {starting to} hate!} {But I {won't give up} so {easy}!} {Especially when someone is {gonna help me}.}
我想创建一个包含这四个条目的数组:
Super duper {extra} text.
Which I'm really {starting to} hate!
But I {won't give up} so {easy}!
Especially when someone is {gonna help me}.
我尝试了两种方法,一种是 preg_split,但效果不佳:
$key = preg_split('/([!?.]{1,3}\} \{)/',$key, -1, PREG_SPLIT_DELIM_CAPTURE);
$sentences = array();
for ($i=0, $n=count($key)-1; $i<$n; $i+=2) {
$sentences[] = $key[$i].$key[$i+1]."<br><br>";
}
另一个使用 preg_match_all 非常好,直到我意识到我有这些括号多级:
$matches = array();
$key = preg_match_all('/\{[^}]+\}/', $key, $matches);
$key = $matches[0];
提前致谢!:)