The str_replace() function replaces some characters with some other characters in a string.
str_replace()函数的作用是:将某个子字符串都替换为另一个字符串(大小写不敏感)。
This function works by the following rules:
这个函数必须遵循下列原则:
str_replace(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-sensitive. Use str_ireplace() to perform a case-insensitive search.
注意:str_replace()函数函数是区分大小写的。如果不需要对大小写加以区分,那么可以使用str_irreplace()函数,因为这个函数是不区分大小写的。
Note: This function is binary-safe.
注意:这个函数是“二进制精确的[binary-safe]”。
The output of the code above will be:
上述代码将输出下面的结果:
In this example we will demonstrate str_replace() with an array and a count variable:
在下面的例子中,我们我们通过一个数组和一个count变量演示了str_ireplace()函数:
The output of the code above will be:
上述代码将输出下面的结果:
In this example we will demonstrate str_replace() with less elements in replace than find:
在下面的例子中,我们演示了当使用str_replace()函数时,指定替代的元素少于搜索到的元素的情况:
The output of the code above will be:
上述代码将输出下面的结果: