PHP-帮助我基于REGEX的递归函数 [英] PHP - help with my REGEX-based recursive function

查看:104
本文介绍了PHP-帮助我基于REGEX的递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Wikipedia API中提取一个字符串,该字符串最初看起来像这样: preg_match","preg_replace"一起使用. 像这样:

I'm extracting a string from wikipedia API that initially looks like this: link text. I want to peel off all {{...}} and everything in between them (could be any kind of text). For that I thought about using a recursive function with "preg_match","preg_replace". something like:

function drop_brax($text)
{
    if(preg_match('/{{(.)*}}/',$text)) 
    return drop_brax(preg_replace('/{{(.)*}}/','',$text));
    return $text;
}

由于以下情况,该功能将无法使用:

This function will not work because of a situation like this:

{{我喜欢mocachino {{但我也喜欢 香蕉}}和水果}}

{{ I like mocachino {{ but I also like banana}} and frutis }}

这将剥离{{和}}首次出现之间的所有内容(并省略"and frutis}}"). 如何正确执行此操作? (同时保持良好的递归形式).

this will peel off everything between the first occurence of both {{ and }} (and leave out "and frutis }}"). How can I do this properly? (while maintaining the nice recursive form).

推荐答案

尝试如下操作:

$text = '...{{aa{{bb}}cc}}...{{aa{{bb{{cc}}bb{{cc}}bb}}dd}}...';
preg_match_all('/\{\{(?:[^{}]|(?R))*}}/', $text, $matches);
print_r($matches);

输出:

Array
(
    [0] => Array
        (
            [0] => {{aa{{bb}}cc}}
            [1] => {{aa{{bb{{cc}}bb{{cc}}bb}}dd}}
        )
)

简短说明:

\{\{      # match two opening brackets
(?:       # start non-capturing group 1
  [^{}]   #   match any character except '{' and '}'
  |       #   OR
  (?R)    #   recursively call the entire pattern: \{\{(?:[^{}]|(?R))*}}
)         # end non-capturing group 1
*         # repeat non-capturing group 1 zero or more times
}}        # match two closing brackets

这篇关于PHP-帮助我基于REGEX的递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆