I have strings like this
Foo(2)moreFoo (1)
Foo(1)moreFoo (2)
Foo(99)moreFoo (3)
Foo(99)moreFoo (9)
Foo
I need to take each string, and change the last (number)
So, in this case, I would like to get for each string:
Foo(2)moreFoo (2)
Foo(1)moreFoo (3)
Foo(99)moreFoo (4)
Foo(99)moreFoo (10)
Foo(1)
It would be like get the string, get the last occurence of (number)
, and replace it by (number +1)
How can I do this?
I've checked this answer, but not sure how to change it.
I've started with
$subject = 'Foo (2)(1)';
if(preg_match('\([0-9]+\)', $subject))
{
$pos = strrpos($subject, '\([0-9]+\)');
if($pos !== false)
{
$subject = substr_replace($subject, $replace, $pos, strlen($search));
}
}
but the problem is that could be that the number will be (10) so, I cannot just get the last 3 characters.
The (number)
, if exists, will be at end. It could be at middle, but the interesting one is in the end.