替换某个字符串中的一个或若干个字串为数组中某些值
php本身有自带的函数,可以不用循环非常高效的实现其效果:
实例代码:
1
|
$phrase
= "You should eat fruits , vegetables, and fiber every day." ;<br> $healthy
= array ( "fruits" , "vegetables" , "fiber" );<br> $yummy
= array ( "pizza" , "beer" , "banana"
);<br><br> $newphrase
= str_replace ( $healthy , $yummy , $phrase );<br>
|
print_r($newphrase);结果显示为:
You should eat pizza , beer, and banana every day.