当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > str_ireplace() 函数
The str_ireplace() function replaces some characters with some other characters in a string.
str_ireplace()函数的作用是:将某个子字符串都替换为另一个字符串(大小写不敏感)。
This function works by the following rules:
这个函数必须遵循下列原则:
str_ireplace(find,replace,string,count) |
Parameter参数 | Description描述 |
---|---|
find | Required. Specifies the value to find 必要参数。指定需要查找的值 |
replace | Required. Specifies the value to replace the value in find 必要参数。指定替代值 |
string | Required. Specifies the string to be searched 必要参数。指定需要执行搜索的字符串 |
count | Optional. A variable that counts the number of replacements 可选参数。指定需要执行替换的数量 |
Note: This function is case-insensitive. Use str_replace() to perform a case-sensitive search.
注意:str_irreplace()函数函数是不区分大小写的。如果需要对大小写加以区分,那么可以使用str_replace()函数,因为这个函数是区分大小写的。
Note: This function is binary-safe.
注意:这个函数是“二进制精确的[binary-safe]”。
<?php echo str_ireplace("WORLD","Peter","Hello world!"); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Hello Peter! |
In this example we will demonstrate str_ireplace() with an array and a count variable:
在下面的例子中,我们我们通过一个数组和一个count变量演示了str_ireplace()函数:
<?php $arr = array("blue","red","green","yellow"); print_r(str_ireplace("RED","pink",$arr,$i)); echo "Replacements: $i"; ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Array ( [0] => blue [1] => pink [2] => green [3] => yellow ) Replacements: 1 |
In this example we will demonstrate str_ireplace() with less elements in replace than find:
在下面的例子中,我们演示了当使用str_ireplace()函数时,指定替代的元素少于搜索到的元素的情况:
<?php $find = array("HELLO","WORLD"); $replace = array("B"); $arr = array("Hello","world","!"); print_r(str_ireplace($find,$replace,$arr)); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Array ( [0] => B [1] => [2] => ! ) |