PHP str_replace()和str_ireplace()区别与解释
php 的str_replace() 函数和str_ireplace在程序中用得是比较多的一个函数,它的主要功能是使用一个字符串替换字符串中的另一些字符,下面是它的具体使用
str_replace函数对大小写敏感。而使用 str_ireplace() 就可以执行对大小写不敏感的搜索,其它的应该就没有什么区别了
str_replace(find,replace,string,count)
find | 必需。规定要查找的值。 |
replace | 必需。规定替换 find 中的值的值。 |
string | 必需。规定被搜索的字符串。 |
count | 可选。一个变量,对替换数进行计数。 |
<?phpecho DE>str_replace("world","John","Hello world!")DE>;?>
输出:
立即学习“PHP免费学习笔记(深入)”;
Hello John!
在本例中,我们将演示带有数组和 count 变量的 str_replace() 函数:
<?php$arr = array("blue","red","green","yellow");print_r(DE>str_replace("red","pink",$arr,$i)DE>);echo "Replacements: $i";?>
输出:
立即学习“PHP免费学习笔记(深入)”;
Array([0] => blue[1] => pink[2] => green[3] => yellow)Replacements: 1
<?php$find = array("Hello","world");$replace = array("B");$arr = array("Hello","world","!");print_r(DE>str_replace($find,$replace,$arr)DE>);?>
输出:
立即学习“PHP免费学习笔记(深入)”;
Array([0] => B[1] =>[2] => !)
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号