用递增的计数器值替换子字符串 [英] Replace substrings with an incremented counter value
本文介绍了用递增的计数器值替换子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上我正在寻找的是该线程的PHP版本:在每次出现字符串时查找,替换和递增
Basically what I'm looking for is the PHP version of this thread: Find, replace, and increment at each occurence of string
我想在rach行的开头将>
之后的关键字替换为递增计数器.
I would like to replace the keyword following >
at the start of rach line with an incrementing counter.
如果我输入的是:
>num, blah, blah, blah
ATCGACTGAATCGA
>num, blah, blah, blah
ATCGATCGATCGATCG
>num, blah, blah, blah
ATCGATCGATCGATCG
我希望成为...
>0, blah, blah, blah
ATCGACTGAATCGA
>1, blah, blah, blah
ATCGATCGATCGATCG
>2, blah, blah, blah
ATCGATCGATCGATCG
推荐答案
$str = 'a hello a some a';
$i = 0;
while (strpos($str, 'a') !== false)
{
$str = preg_replace('/a/', $i++, $str, 1);
}
echo $str;
这篇关于用递增的计数器值替换子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文