>  기사  >  백엔드 개발  >  PHP str_replace()跟str_ireplace()区别与解释

PHP str_replace()跟str_ireplace()区别与解释

WBOY
WBOY원래의
2016-06-13 13:08:101290검색

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 可选。一个变量,对替换数进行计数。

例子

例子 1

<?php echo DE>str_replace("world","John","Hello world!")DE>;
?>

输出:

Hello John!

例子 2

在本例中,我们将演示带有数组和 count 变量的 str_replace() 函数:

<?php $arr = array("blue","red","green","yellow");
print_r(DE>str_replace("red","pink",$arr,$i)DE>);
echo "Replacements: $i";
?>

输出:

Array
(
[0] => blue
[1] => pink
[2] => green
[3] => yellow
)
Replacements: 1

例子 3

<?php $find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(DE>str_replace($find,$replace,$arr)DE>);
?>

输出:

Array
(
[0] => B
[1] =>
[2] => !
)
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.