這篇文章主要為大家展示了php實(shí)現(xiàn)數(shù)據(jù)替換的方法,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶大家一起來(lái)研究并學(xué)習(xí)一下“php實(shí)現(xiàn)數(shù)據(jù)替換的方法”這篇文章吧。
php實(shí)現(xiàn)數(shù)據(jù)替換的方法:首先創(chuàng)建一個(gè)PHP示例文件;然后通過(guò)“str_replace”函數(shù)實(shí)現(xiàn)字符串替換字符串或者數(shù)組替換數(shù)組即可。
PHP字符串替換str_replace()函數(shù)四種用法
// 參數(shù) $search,要替換的字符串,或數(shù)組 // 參數(shù) $replace,被用來(lái)替換的字符串或數(shù)組 // 參數(shù) $subject,被查詢的字符串或數(shù)組 // 參數(shù) $count,可選,如果被指定,將為設(shè)置為替換的次數(shù) // 返回值:該函數(shù)返回替換后的數(shù)組或者字符串(新生成的) //實(shí)例一:字符串替換字符串 $str1 = str_replace("red","black","red green yellow pink purple"); echo $str1.""; //輸出結(jié)果為black green yellow pink purple //實(shí)例二:字符串替換數(shù)組鍵值 $arr = array("blue","red","green","yellow"); $str1 = str_replace("red","pink",$arr,$i); print_r($str1); //實(shí)例三:數(shù)組替換數(shù)組,映射替換 $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); //實(shí)例四:如$search為數(shù)組,$replace為字符串時(shí) $search = array("banana","grape"); $replace = "tomato"; $arr = array("banana","apple","orange","grape"); $new_arr = str_replace($search,$replace,$arr,$count); print_r($new_arr);
以上就是關(guān)于“php實(shí)現(xiàn)數(shù)據(jù)替換的方法”的內(nèi)容,如果改文章對(duì)你有所幫助并覺得寫得不錯(cuò),勞請(qǐng)分享給你的好友一起學(xué)習(xí)新知識(shí),若想了解更多相關(guān)知識(shí)內(nèi)容,請(qǐng)多多關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。