我已經建立了一個非常簡單的代碼片段:爲什麼在幾個str_replace()函數中只有最後一個影響字符串?
$string = 'Some random words. Some more random, very random words.';
$words = explode(" ", $string);
for ($i = 0; $i < count($words); $i++) {
$word = $words[$i];
$words[$i] = str_replace(".", "!", $word);
$words[$i] = str_replace(",", "?", $word);
}
print_r($words);
輸出是這樣的:
Array
(
[0] => Some
[1] => random
[2] => words.
[3] => Some
[4] => more
[5] => random?
[6] => very
[7] => random
[8] => words.
)
爲什麼只有第二str_replace()
功能影響的字符串?如果我刪除第二個str_replace()
第一個完美的作品。這不是關於使用str_replace()
,但我相信我做了一件非常非常非常錯誤的事情。
順便說一句 - 我知道preg_replace()
並將數組傳遞給str_replace()
但希望瞭解這種特殊情況:)。
編輯: 謝謝大家的快速反應。我對這樣的問題感到羞愧,但起初它確實沒有吸引我的眼球。感謝大家!我會接受Mike Brant的第一個正確答案。
哦,我笨! –