我有这个代码:
$count = 0;
preg_replace('/test/', 'test'. $count, $content,-1,$count);
Run Code Online (Sandbox Code Playgroud)
对于每次替换,我获得test0.
我想得到test0,test1,test2等.
如何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是一个正则表达式.这两个都很明确.
谢谢你的帮助.