时间:2021-07-01 10:21:17 帮助过:38人阅读
php中替换字符串的实现方法:首先创建一个PHP示例文件;然后通过“str_replace("red","black","red green yellow pink purple");”方法替换字符串;最后通过echo输出替换结果即可。
推荐:《PHP视频教程》
该方法适用于所有品牌电脑。
PHP字符串替换str_replace()函数四种用法
参数 $search,要替换的字符串,或数组
参数 $replace,被用来替换的字符串或数组
参数 $subject,被查询的字符串或数组
参数 $count,可选,如果被指定,将为设置为替换的次数
返回值:该函数返回替换后的数组或者字符串(新生成的)
实例一:字符串替换字符串
$str1 = str_replace("red","black","red green yellow pink purple"); echo $str1.""; //输出结果为black green yellow pink purple
实例二:字符串替换数组键值
$arr = array("blue","red","green","yellow"); $str1 = str_replace("red","pink",$arr,$i); print_r($str1);
实例三:数组替换数组,映射替换
$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);
实例四:如$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中替换字符串的实现方法的详细内容,更多请关注gxlcms其它相关文章!