I have array of vars
array(2) {
["M"]=>
string(3) "500"
["MA"]=>
string(3) "200"
}
AND strings "1000-M", "1000-MA", "1000+MA-M", "MA/2", "M*MA+100"
And I need to replace M with value 500, and MA with value 200. When I use str_replace it works but only for M, because it still match with first var M
Thanks for help
2
Answers
You can use
preg_replace_callback()
function to match the variable names using regex.You can sort your replace array by longest keyword first, so that it will be replaced first, and only later on replace shorter keys:
Example