If you had the string
'Old string Old more string Old some more string'
and you wanted to get
'New1 string New2 more string New3 some more string'
how would you do it?
In other words, you need to replace all instances of ‘Old’ with variable string ‘New’.$i. How can it be done?
An iterative solution that doesn’t need regular expressions:
$offsetinstrpos()is just a little bit micro-optimization. I don’t know, if it’s worth it (in fact I don’t even know, if it changes anything), but the idea is that we don’t need to search for$oldin the substring that is already processed.See Demo