使用阵列替换Preg_replace
$string = ":abc and :def have apples.";
$replacements = array('Mary', 'Jane');
应变为:
Mary and Jane have apples.
现在我是这样做的:
preg_match_all('/:(\w+)/', $string, $matches);
foreach($matches[0] as $index => $match)
$string = str_replace($match, $replacements[$index], $string);
我可以在一次运行中执行此操作,使用类似preg_replace?