好吧,我有一個str_replace,我想做的是從數組中取值,並採取下一塊取代單詞「狗」。所以基本上我想要的$字符串爲:PHP str_replace與從循環陣列
「鴨子吃了貓和豬吃黑猩猩」
<?php
$string = 'The dog ate the cat and the dog ate the chimp';
$array = array('duck','pig');
for($i=0;$i<count($array);$i++) {
$string = str_replace("dog",$array[$i],$string);
}
echo $string;
?>
此代碼只是返回:
「鴨子吃了貓鴨吃了黑猩猩「
我嘗試了幾件事情,但沒有任何工作。有人有主意嗎?
我猜你可以使用'strstr'和'substr_replace'的組合。 – mpen 2009-11-14 06:09:33