Home > Article > Backend Development > php string replacement function_PHP tutorial
This article introduces two character replacement functions for PHP beginners, one is str_ireplace() and the other is substr_replace(). These two functions are relatively easy to use. Please refer to them if necessary.
String replacement technology can be implemented through the following two commonly used functions: str_ireplace() function and substr_replace() function
str_ireplace() function
Use a new substring to replace the string specified to be replaced in the original string, syntax:
mixed str_ireplace(mixed search,mixed replace,mixed subject[,int&count])
Parameter search: necessary parameter, specify the string to be searched.
Parameter replace: necessary parameter, specify the replacement value.
Parameter subject: necessary parameter, specifying the search range.
Parameter count: optional parameter, (the ones with square brackets are optional parameters), get the number of substitutions to be performed.
Example:
The code is as follows | Copy code |
代码如下 | 复制代码 |
$str2=”某某”; $str1=”**”; $str=”某某网站的地址是www.hzhuti.com ,某某网站主要记录一些学习php的笔记和感想以及各种软件知识”; echo str_ireplace($str2,$str1,$str); //str2查找的值,str1替换的值,str范围 ?> |
$str1="**";
$str=”The address of such-and-such website is www.hzhuti.com. Such-and-such website mainly records some notes and reflections on learning PHP and various software knowledge”;代码如下 | 复制代码 |
$arr = array("blue","red","green","yellow"); 输出: Array |
?>
In this example we will demonstrate the str_ireplace() function with an array and count variable:代码如下 | 复制代码 |
$find = array("Hello","world"); Array
|
The code is as follows | Copy code | ||||
$arr = array("blue","red","green","yellow");
|
The code is as follows | Copy code |
$find = array("Hello","world");<🎜> $replace = array("B");<🎜> $arr = array("Hello","world","!");<🎜> print_r(str_ireplace($find,$replace,$arr));<🎜> ?>Output: Array ( [0] => B [1] => [2] => ! ) |
The code is as follows | Copy code |
substr_replace('eggs','x',-1,-1); //eggxs <🎜> substr_replace('eggs','x',-1,-2); //eggxs <🎜> substr_replace('eggs','x',-1,-2); //eggxs <🎜> ?> Same as: substr_replace('eggs','x',-1,0); //eggxs <🎜> ?> substr_replace('huevos','x',-2,-2); //huevxos <🎜> substr_replace('huevos','x',-2,-3); //huevxos <🎜> substr_replace('huevos','x',-2,-3); //huevxos <🎜> ?> Same as: substr_replace('huevos','x',-2,0); //huevxos <🎜> ?> |
For more details, please check: http://www.bKjia.c0m/phper/21/32954.htm