1.背景

对字符串实现一对多替换,并返回替换次数。

2.函数示例

str_replace() 函数替换字符串中的一些字符(区分大小写),返回带有替换值的字符串或数组。如下:

$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "替换数:$i";

3.批量替换

$find = array("Hello","world");
$replace = array("B","A");
$arr = array("Hello world","world","!");
$str = "Hello world danbus world";
print_r(str_replace($find,$replace,$arr));
//批量替换数组输出
//Array(
//[0] => B A
//[1] => A
//[2] => !
//)
print_r(str_replace($find,$replace,$str));
//批量替换字符串后输出
//B A danbus A