[英]Using an array in preg_replace to reduce codes
是否可以在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
混合的preg_replace( 混合 $模式, 混合 $更換,混合$主題,... 的preg_replace
$text = "1 2 3";
$array = array(
'/1/' => "one",
'/2/' => "two",
'/3/' => "three");
$text = preg_replace(array_keys($array), $array, $text);
eval.in上的演示 (如評論中所述,此處無需使用正則表達式)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.