推 alpe :preg_replace(array('/1/', '/2/'), array('bob', )) 06/15 17:33
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.87.174
事情是如此的…
如果有一個字串…
$str = " this is a cat <1,2> ; that is a dog <2,3> "
另外…還有一個陣列…
$array = array("Zero","Alice","Bob","Carl");
那…我能否用一次preg_replace,
就把$str中<數字1, 數字2> 取代成 $array[數字1]-$array[數字2]?
例如上列的 $str 經由 preg_replace時…
會變成 " this is a cat Alice-Bob ; that is a dog Bob-Carl " 降子
嗯嗯…如果是javascript的話…好像就作得到…
因為他的replacement可以傳入一個function
但…php好像就不允許人家這樣作…
那…php有替代的作法嗎?
我已經找過了preg_replace的資料><~但我的需求變得關鍵字不知道怎麼下><~
先感謝強者回答了0rz...
--