相关疑难解决方法(0)

使用preg_replace时如何增加替换字符串中的计数?

我有这个代码:

$count = 0;    
preg_replace('/test/', 'test'. $count, $content,-1,$count);
Run Code Online (Sandbox Code Playgroud)

对于每次替换,我获得test0.

我想得到test0,test1,test2等.

php regex string preg-replace

10
推荐指数
2
解决办法
9840
查看次数

在preg_replace_callback中使用局部变量 - PHP

如何preg_replace_callback在PHP中使用局部变量.我有以下代码:

function pregRep($matches)
{
    global $i; $i++;

    if($i > 2)
    {     
          return '#'.$matches[0];
    }
    else
    {
        return $matches[0];
    }
}

$i = 0;
$str =  preg_replace_callback($reg_exp,"pregRep",$str); 
Run Code Online (Sandbox Code Playgroud)

而且也是$str一个字符串,$reg_exp是一个正则表达式.这两个都很明确.

谢谢你的帮助.

php preg-replace-callback

1
推荐指数
1
解决办法
1898
查看次数

标签 统计

php ×2

preg-replace ×1

preg-replace-callback ×1

regex ×1

string ×1