我有一个字符串,其中包含几个用花括号括起来的句子,我想要删除这些括号。这看起来并不难(现在我知道了),但真正困难的是它是多层级的,并且我只想去掉顶层括号,保留里面的所有内容。它看起来像这样:
{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];
提前感谢您!:)