首頁  >  文章  >  後端開發  >  php如何替換陣列裡的字串

php如何替換陣列裡的字串

王林
王林原創
2021-03-04 17:35:403261瀏覽

php替換陣列裡的字串的方法:可以利用str_replace函數來進行替換,如【str_replace("red","pink",$arr,$i)】。如果搜尋的字串是一個數組,那麼它將對數組中的每個元素進行查找和替換。

php如何替換陣列裡的字串

本文操作環境:windows10系統、php 7.3、thinkpad t480電腦。

如果我們需要替換數組中的字串,可以利用str_replace()函數。

提示:如果搜尋的字串是數組,那麼它將對數組中的每個元素進行查找和替換。

語法:

str_replace(find,replace,string,count)

參數介紹:

  • find    必要。規定要找的值。   

  • replace    必需。規定替換 find 中的值的值。   

  • string    必需。規定被搜尋的字串。   

  • count    可選。一個變量,對替換數進行計數。

程式碼範例:

<!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>

(學習影片分享:php影片教學

執行結果:

php如何替換陣列裡的字串

相關推薦:php教學

以上是php如何替換陣列裡的字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn