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

时间:2013-03-09 21:44:03

标签: php

假设我有这句话:“奥巴马总统和他的家人从非洲回到美国。”

如何将“obama”替换为“bush”?

2 个答案:

答案 0 :(得分:3)

使用str_replace()函数:

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

答案 1 :(得分:1)

这是一个愚蠢的问题。如果你只是用谷歌搜索它,就会找到解决方案:

  

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);