PHP/Regex -抓取{和}之间的所有内容


PHP/Regex - Grab everything between { and }?

我有一些像这样的文本字符串

{你好|嗨}{你}|

我想计算{…},所以在上面的例子中,我想返回:


你好|嗨|你

preg_match_all()

创建的匹配数组中

现在我的代码看起来像:

preg_match_all('/{(.*?)}/', $text,$text_pieces);

和$text_pieces包含:

Array ( [0] => Array ( [0] => {hello|hi} [1] => {there|you} ) [1] => Array ( [0] => hello|hi [1] => there|you ) )

我只需要这个:

[0] => hello|hi [1] => there|you 

preg_match_all不能省略全文匹配,只能省略子模式匹配,因此唯一的解决方案是在函数调用后将$text_pieces设置为$text_pieces[1]:

if(preg_match_all('/{(.*?)}/', $text,$text_pieces))
{
    $text_pieces = $text_pieces[1];
}