I have a string $src with two variable placeholders %%itemA%% and %%itemB%%. The idea is for the regular expression to match any term listed ITEMA|ITEMB|ITEMC, case insensitively, and replace the term with the value of the array item using the term as the array key.
$replacements = [
'itemA' => 'replacedItemA',
'itemB' => 'replacedItemB',
'itemC' => 'replacedItemC'
];
$src = "A B C %%itemA%% D E F %%itemB%% G H I";
$src = preg_replace('/\%\%(ITEMA|ITEMB|ITEMC)%%/i', $replacements['\1'], $src);
echo $src;
In this example the expected result is for %%itemA%% to be replaced with $replacements['itemA']'s value and %%itemB%% to be replaced with $replacements['itemB']'s value
Expected output from the echo was
A B C replacedItemA D E F replacedItemB G H I
Actual output, oddly, simply replaced the found terms with nothing (note the double spaces where the variable was removed)
A B C D E F G H I
Why is the term's string not being used in the $replacements['key'] to use the value of the array variable?