这篇文章给大家分享的是有关php使用替换字符串函数的方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

php使用替换字符串函数的方法:1、【$search】要替换的字符串,或数组;2、【$replace】被用来替换的字符串或数组;3、【$subject】被查询的字符串或数组;4、【$count】可选,如果被指定,将为设置为替换的次数。

php使用替换字符串函数的方法:

PHP字符串替换str_replace()函数4种用法,具体内容如下所示:

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

该函数返回一个字符串或者数组。该字符串或数组是将subject中全部的search都被replace替换之后的结果。

1、$search,要替换的字符串,或数组

2、$replace,被用来替换的字符串或数组

3、$subject,被查询的字符串或数组

4、$count,可选,如果被指定,将为设置为替换的次数

5、返回值:该函数返回替换后的数组或者字符串(新生成的)

<?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使用替换字符串函数的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!