是否可以在preg_replace中使用数组来减少所需的代码?所以,像这样:
$text = "1 2 3";
$array = array(1 => "one", 2 => "two", 3 => "three");
preg_replace($array, $text);
echo $text;
结果:
1 2 3
需要的结果:
one two three
答案 0 :(得分:0)
混合preg_replace(混合 $ pattern,混合 $替换,混合$ subject,... preg_replace
$text = "1 2 3";
$array = array(
'/1/' => "one",
'/2/' => "two",
'/3/' => "three");
$text = preg_replace(array_keys($array), $array, $text);
Demo at eval.in(如评论中所述,此处无需使用正则表达式)