Home  >  Article  >  Backend Development  >  php string replacement function_PHP tutorial

php string replacement function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:55:23797browse

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范围
?>
$str2=”Someone”;

$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");
print_r(str_ireplace("red","pink",$arr,$i));
echo "Replacements: $i";
?>

输出:

Array
(
[0] => blue
[1] => pink
[2] => green
[3] => yellow
)

echo str_ireplace($str2,$str1,$str); //The value found by str2, the value replaced by str1, str range

?>

In this example we will demonstrate the str_ireplace() function with an array and count variable:
 代码如下 复制代码

$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_ireplace($find,$replace,$arr));
?>输出:

Array
(
[0] => B
[1] =>
[2] => !
)


 

The code is as follows Copy code

$arr = array("blue","red","green","yellow");
print_r(str_ireplace("red","pink",$arr,$i));
echo "Replacements: $i";
?>

Output:

Array
 代码如下 复制代码

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
?>

( [0] => blue [1] => pink [2] => green [3] => yellow )
Replacements: 1Example 3
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] => ! )
substr_replace() function Replace part of the string in the specified string, syntax: string substr_replace(string str,string repl,int start,[int length]) Parameter str: Specifies the original string to be operated on. Parameter repl: a necessary parameter, specifying the new string after replacement. Parameter start: Specifies the starting position of the replacement string. Parameter length: Specifies the length of the returned string. Example:
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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631687.htmlTechArticleThis article tells PHP beginners about two example character replacement functions in PHP, one is str_ireplace() and the other The two functions of substr_replace() are relatively easy to use. Please refer to them if necessary. Characters...
Statement:
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