我需要用增量计数替换某个模式。我的代码:
$text = "Hello (apple) hello (banana) hello (cucumber)";
$pattern = '/\(([^\)]*)\)/s';
$i = 0;
$returnText = preg_replace_callback($pattern, function($matches) use ($i) {
return '<sup>['.$i++.']</sup>';
}, $text);
echo $returnText;
结果:
Hello [0] hello [0] hello [0]
我需要的:
Hello [0] hello [1] hello [2]
我究竟做错了什么?