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

王林
王林原创
2023-03-08 12:42:022622浏览

php替换数组里的字符串的方法:可以利用str_replace函数来进行替换,如【str_replace("red","pink",$arr,$i)】。如果搜索的字符串是一个数组,那么它将对数组中的每个元素进行查找和替换。

本文操作环境: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视频教程

运行结果:

e7e475a62a70c8c2a1163b7b2287e61.png

相关推荐:php教程

以上就是php如何替换数组里的字符串的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。