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

WBOY
Release: 2016-06-13 10:56:39
Original
969 people have browsed it

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

PHP 的str_replace() 函数和str_ireplace在程序中用得是比较多的一个函数,它的主要功能是使用一个字符串替换字符串中的另一些字符,下面是它的具体使用

Str_replace函数对大小写敏感。而使用 str_ireplace() 就可以执行对大小写不敏感的搜索,其它的应该就没有什么区别了

语法

str_replace(find,replace,string,count)
Copy after login
参数描述
find 必需。规定要查找的值。
replace 必需。规定替换 find 中的值的值。
string 必需。规定被搜索的字符串。
count 可选。一个变量,对替换数进行计数。

例子

例子 1

<?phpecho DE>str_replace("world","John","Hello world!")DE>;?>
Copy after login

输出:

Hello John!
Copy after login

例子 2

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

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

输出:

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

例子 3

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

输出:

Array([0] => B[1] =>[2] => !)
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!