Replace substrings with an incremented counter value

后端 未结 5 1917
耶瑟儿~
耶瑟儿~ 2021-01-17 08:25

Basically what I\'m looking for is the PHP version of this thread: Find, replace, and increment at each occurence of string

I would like to replace the keyword follow

5条回答
  •  既然无缘
    2021-01-17 09:14

    preg_replace(array_fill(0, 5, '/'.$findme.'/'), range(1, 5), $string, 1);
    

    Example:

    preg_replace(array_fill(0, 5, '/\?/'), range(1, 5), 'a b ? c ? d ? e f g ? h ?', 1);
    

    Output

    a b 1 c 2 d 3 e f g 4 h 5
    

提交回复
热议问题