$myNumber plus


$myNumber=100;


$myVar='myVar is[COLOR="#FF0000"] plus5[/COLOR] which is '.$myNumber.'+5
myVar is [COLOR="#FF0000"]plus10[/COLOR] which is '.$myNumber.'+10
myVar is [COLOR="#FF0000"]plus20[/COLOR] which is '.$myNumber.'+20';

I have a number variable “$myNumber” which is like the above.

I like to replace the text plus5 to 105, the text plus10 to 110 and the text plus20 to 120.

So my target result will be like the following.

[b]target result[/b]

$myTarget_result='myVar is [COLOR="#FF0000"]105[/COLOR] which is 100+5
'myVar is [COLOR="#FF0000"]110[/COLOR] which is 100+10
myVar is [COLOR="#FF0000"]120[/COLOR] which is 100+20';


How can I get $myTarget_result from $myVar?

Using str_replace?

$myTarget_result = str_replace(array('plus5', 'plus10', 'plus20'), array($myNumber + 5, $myNumber + 10, $myNumber+20), $myTarget_result);

This is assuming you weren’t wanting to concatenate the string (like you did with $myNumber)

$myVar='myVar is '.($myNumber+5).' which is '.$myNumber.'+5
myVar is '.($myNumber+10).' which is '.$myNumber.'+10
myVar is '.($myNumber+20).' which is '.$myNumber.'+20';