I would like to replace a pattern string '/r/' within a subject string 'r r' with predetermined replacements like '"\\23"' and '[4]'. So (showing you only a subset of my replacements array) I try:
$subject = 'r r';
$replacements = array('"\\23"', '[4]');
$replacements = array_map('preg_quote', $replacements);
print_r($replacements);
foreach($replacements as $replacement){
$subject = preg_replace('/r/', $replacement, $subject, 1);
}
echo $subject;
The string I want is '"\\23" [4]', but I get back '"\23" \[4\]', which is both missing characters and has additional characters.
Does PHP have an escape function that would work here? How do I do this?
Note: Without the preg_quote line the result is '"" [4]'.