I want to ask if it's possible to use a matched regex pattern in determining the replacement from an array. For example
$rpl['brat'] = 'qwerty';
$rpl['omri'] = 'asdfgh';
$str1 = 'abc brat bca';
$str2 = 'abc omri bca';
print_r(preg_replace('#bc (.+?) bc#'), $rpl[$1], $str1)); // aqwertya
print_r(preg_replace('#bc (.+?) bc#'), $rpl[$1], $str2)); // aasdfgha
Now obviously $1
is incorrect syntax, but this is just to show the point I'm making. How can I do this?