如何知道preg_replace_callback中的匹配计数 - PHP

san*_*a26 1 php preg-replace-callback

我在php中有这个代码 - :

function pregRepler($matches)
{
    * do something
}

$str = preg_replace_callback($reg_exp,'pregRepler',$str);
Run Code Online (Sandbox Code Playgroud)

在功能上pregRepler,我想知道当前的情况match number,如果它是第一场比赛还是第二场比赛......

我该怎么做.??

Nie*_*sol 6

尝试这样的事情:

function pregRepler($matches) {
    static $matchcount = 0;
    // do stuff
    $matchcount++;
}
Run Code Online (Sandbox Code Playgroud)

这与一个匿名函数的工作更好,因为我提到我的回答您的其他问题,因为这将避免出现问题,如果你有多个电话preg_replace_callback.