I have a string containing comma-separated numbers and I also have an associative array in PHP containing unique numbers. What I want to achieve is to create a new string that contains only the exact match replacements based on the array. Here is my code so far:
<?php
$replacements = array(
'12' => 'Volvo',
'13' => 'BMW',
'132' => 'Alfa Romea',
'156' => 'Honda',
'1536' => 'Tesla',
'2213' => 'Audi'
);
$str ="12,13,132,2213";
echo str_replace(array_keys($replacements), $replacements, $str);
?>
The only problem is that the output is this:
Volvo,BMW,BMW2,22BMW
instead of this:
Volvo,BMW,Alfa Romeo,Audi
How can I achieve exact match search and replace in this function? Is there a fastest PHP solution for this problem? Thanks, belf