使用 preg_replace 时如何增加替换字符串中的计数? [英] How to increment count in the replacement string when using preg_replace?
本文介绍了使用 preg_replace 时如何增加替换字符串中的计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码:
$count = 0;
preg_replace('/test/', 'test'. $count, $content,-1,$count);
对于每次替换,我都会获得 test0.
For every replace, I obtain test0.
我想得到 test0、test1、test2 等等.
I would like to get test0, test1, test2 etc..
推荐答案
$count = 0;
preg_replace_callback('/test/', 'rep_count', $content);
function rep_count($matches) {
global $count;
return 'test' . $count++;
}
这篇关于使用 preg_replace 时如何增加替换字符串中的计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文