函数名:str_replace()
适用版本:所有PHP版本(PHP 4,PHP 5,PHP 7)
用法:替换字符串中的部分内容
语法: str_replace($search, $replace, $subject, $count)
参数:
返回值:替换后的字符串或字符串数组。
示例1:
$str = "Hello, World!";
$newStr = str_replace("World", "PHP", $str);
echo $newStr; // 输出:Hello, PHP!
示例2:
$search = array("blue", "red", "green");
$replace = array("yellow", "purple", "orange");
$str = "The sky is blue, the rose is red, and the grass is green.";
$newStr = str_replace($search, $replace, $str);
echo $newStr; // 输出:The sky is yellow, the rose is purple, and the grass is orange.
示例3:
$search = array("apple", "banana", "orange");
$replace = "fruit";
$str = "I like apple, banana, and orange.";
$newStr = str_replace($search, $replace, $str, $count);
echo $newStr; // 输出:I like fruit, fruit, and fruit.
echo $count; // 输出:3
注意事项: