PHP中的字符串替换(str_replace)

 /*替换

字符串处理  str_replace()

*/

$num = 0; 

$str = "http://www.phpbrother.net/php/demo.php";
$str1="这是一句正常的寒霜句子,错误但里面有一些不能显示的文字";
//在$str里搜索php,替换成java
$newstr1=str_replace("php","java",$str);
$newstr2=str_replace("php","java",$str,$num);

//要替换掉正常,寒霜,错误
//多个词换成一个词
$newstr3 = str_replace(array("正常","寒霜","错误"), "**", $str1, $num);
//多个词换成多个词,前面词数量多会自动替换成空格,后面多不会有任何效果
$newstr3 = str_replace(array("正常","寒霜","错误"),array("不正常","不是寒霜","正确"), $str1, $num);

echo $str."<br>";
echo $newstr."<br>";

echo "替换的次数为:{$num}<br>";

posted on 2016-03-19 10:39  奋斗的寒霜  阅读(812)  评论(0)    收藏  举报