?php
//实例一:字符串替换字符串
$str1 = str_replace("red","black","red green yellow pink purple");
echo $str1.""; //输出结果为black green yellow pink purple
?>
?php
//实例二:字符串替换数组键值
$arr = array("blue","red","green","yellow");
$str1 = str_replace("red","pink",$arr,$i);
print_r($str1);
?>
?php
//实例三:数组替换数组,映射替换
$arr1 = array("banana","orange");
$arr2 = array("pitaya","tomato");
$con_arr = array("apple","orange","banana","grape");
$con_rep = str_replace($arr1,$arr2,$con_arr,$count);
print_r($con_rep);
?>
?php
//实例四:如$search为数组,$replace为字符串时
$search = array("banana","grape");
$replace = "tomato";
$arr = array("banana","apple","orange","grape");
$new_arr = str_replace($search,$replace,$arr,$count);
print_r($new_arr);
?>
以上所述是小编给大家介绍的PHP字符串替换str_replace()函数四种用法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!