php替換數(shù)組里的字符串的方法:可以利用str_replace函數(shù)來進(jìn)行替換,如【str_replace("red","pink",$arr,$i)】。如果搜索的字符串是一個(gè)數(shù)組,那么它將對(duì)數(shù)組中的每個(gè)元素進(jìn)行查找和替換。
本文操作環(huán)境:windows10系統(tǒng)、php 7.3、thinkpad t480電腦。
如果我們需要替換數(shù)組中的字符串,可以利用str_replace()函數(shù)。
提示:如果搜索的字符串是一個(gè)數(shù)組,那么它將對(duì)數(shù)組中的每個(gè)元素進(jìn)行查找和替換。
語法:
str_replace(find,replace,string,count)
參數(shù)介紹:
-
find 必需。規(guī)定要查找的值。
-
replace 必需。規(guī)定替換 find 中的值的值。
-
string 必需。規(guī)定被搜索的字符串。
-
count 可選。一個(gè)變量,對(duì)替換數(shù)進(jìn)行計(jì)數(shù)。
代碼示例:
<!DOCTYPE html> <html> <body> <?php $arr = array("blue","red","green","yellow"); print_r(str_replace("red","pink",$arr,$i)); echo "<br>" . "Replacements: $i"; ?> <p>In this example, we search an array to find the value "red", and then we replace the value "red" with "pink".</p> </body> </html>
(學(xué)習(xí)視頻分享:php視頻教程)
運(yùn)行結(jié)果: