以下透過本文給大家分享PHP字串取代str_replace()函數4種用法,具體內容如下:
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
該函數傳回一個字串或陣列。該字串或陣列是將subject中全部的search都被replace替換之後的結果。
1、$search,要替換的字串,或陣列(推薦學習:PHP程式設計從入門到精通)
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替換字串用什麼函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!