function multipleReplace($search,$subject,$replace) {
preg_match_all($search, $subject,$matches,PREG_OFFSET_CAPTURE);
foreach($matches as $match) {
if (is_array($match)) {
foreach ($match as $submatch) {
list($string,$start) = $submatch;
$length = strlen($string);
$val = "";
if ($start - 1 > 0) {
$val .= substr($subject,0,$start);
}
$val .= preg_replace($search,$string,$replace);
$val .= substr($subject,$start + $length);
$ret[] = $val;
}
}
}
return $ret;
}
$search = 'a';
print_r(multipleReplace('/\b'.$search.'(?=\s+|$)/u','a b a','1'));
输出
Array
(
[0] => 1 b a
[1] => a b 1
)