PHP preg_replace()反向引用用作另一个函数的参数

Nic*_*ner 1 php regex preg-replace

我试图使用正则表达式从标签中提取信息,然后根据标签的各个部分返回结果.

preg_replace('/<(example )?(example2)+ />/', analyze(array($0, $1, $2)), $src);

所以我抓住零件并将其传递给analyze()函数.在那里,我想基于部件本身做工作:

function analyze($matches) {
    if ($matches[0] == '<example example2 />')
          return 'something_awesome';
    else if ($matches[1] == 'example')
          return 'ftw';
}
Run Code Online (Sandbox Code Playgroud)

但是一旦我进入analyze函数,$matches[0]就等于字符串' $0'.相反,我需要$matches[0]引用preg_replace()调用的反向引用.我怎样才能做到这一点?

谢谢.

编辑:我刚看到preg_replace_callback()函数.也许这就是我要找的......

Ala*_*orm 8

你不能那样用preg_replace.你可能想要preg_replace_callback