如何用php替换句子中的某些单词


how do i replace certain words in a sentence with php

假设我有这样一个句子:"奥巴马总统和他的家人从非洲回到美国"。

如何将"obama"替换为"bush"?

使用str_replace()函数:
$sentence = "The president obama came back to america from africa with his family";
$changed = str_replace("obama", "bush", $sentence);

这是个愚蠢的问题。如果你谷歌一下,就能找到答案了:

str_replace(混合$search,混合$replace,混合$subject [, int &$count])

$subj  = "The president obama came back to america from africa with his family";
$search = "bush";
$replace   = "obama";
echo str_replace($search, $replace, $subj);