站长资讯网
最全最丰富的资讯网站

php如何替换数组里的字符串

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如何替换数组里的字符串

赞(0)
分享到: 更多 (0)