PHP str_ireplace() 函数
PHP 教程
· 2019-01-29 22:43:05
实例
把字符串 "Hello world!" 中的字符 "WORLD"(不区分大小写)替换成 "Peter":
<?php
echo str_ireplace("WORLD","Peter","Hello world!");
?>
echo str_ireplace("WORLD","Peter","Hello world!");
?>
定义和用法
str_ireplace() 函数替换字符串中的一些字符(不区分大小写)。
该函数必须遵循下列规则:
- 如果搜索的字符串是一个数组,那么它将返回一个数组。
- 如果搜索的字符串是一个数组,那么它将对数组中的每个元素进行查找和替换。
- 如果同时需要对某个数组进行查找和替换,并且需要执行替换的元素少于查找到的元素的数量,那么多余的元素将用空字符串进行替换。
- 如果是对一个数组进行查找,但只对一个字符串进行替换,那么替代字符串将对所有查找到的值起作用。
注释:该函数是不区分大小写的。请使用 str_replace() 函数执行区分大小写的搜索。
注释:该函数是二进制安全的。
语法
str_ireplace(find,replace,string,count)
| 参数 | 描述 |
|---|---|
| find | 必需。规定要查找的值。 |
| replace | 必需。规定替换 find 中的值的值。 |
| string | 必需。规定被搜索的字符串。 |
| count | 可选。一个变量,对替换数进行计数。 |
技术细节
| 返回值: | 返回带有替换值的字符串或数组。 |
|---|---|
| PHP 版本: | 5+ |
| 更新日志: | 在 PHP 5.0 中,新增了 count 参数。 |
更多实例
实例 1
使用带有数组和 count 变量的 str_ireplace() 函数:
<?php
$arr = array("blue","red","green","yellow");
print_r(str_ireplace("RED","pink",$arr,$i)); // This function is case-insensitive
echo "Replacements: $i";
?>
$arr = array("blue","red","green","yellow");
print_r(str_ireplace("RED","pink",$arr,$i)); // This function is case-insensitive
echo "Replacements: $i";
?>
实例 2
使用带有需要替换的元素少于查找到的元素的 str_ireplace() 函数:
<?php
$find = array("HELLO","WORLD"); // This function is case-insensitive
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_ireplace($find,$replace,$arr));
?>
$find = array("HELLO","WORLD"); // This function is case-insensitive
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_ireplace($find,$replace,$arr));
?>
点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html
Automate This
Christopher Steiner / Portfolio / 2013-8-9 / USD 25.95
"The rousing story of the last gasp of human agency and how today's best and brightest minds are endeavoring to put an end to it." It used to be that to diagnose an illness, interpret legal docume......一起来看看 《Automate This》 这本书的介绍吧!