PHP中的str_replace()函数
str_replace()函数用于将一个字符串替换为另一个字符串。
注 -该功能区分大小写。
语法
str_replace(find, replace, str, count)
参数
find-要搜索的值
replace-我们要替换$find的字符串
str-要搜索的字符串
count-更换数量
返回
str_replace()函数返回具有替换值的字符串或数组。
示例
以下是一个例子-
<?php
$str = "I am Amit";
$res = str_replace("Amit", "David", $str);
echo $res;
?>输出结果
I am David
示例
以下是一个例子-
<?php
$myArr = array("one","two","three");
print_r(str_replace("three","four",$myArr,$c));
echo "<br>" . "Number of Replacements = $c";
?>输出结果
Array ( [0] => one [1] => two [2] => four ) <br> Number of Replacements = 1
