Heim  >  Artikel  >  Backend-Entwicklung  >  PHP str_replace()跟str_ireplace()区别与解释

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

WBOY
WBOYOriginal
2016-06-13 13:08:101288Durchsuche

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] => !
)
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn