在开发过程中,字符串的操作是很频繁的,这几天有看PHP手册中讲解str_replace函数的讲解,不是很明白,特作如下笔记,以作备忘。
函数原型:mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
参数说明:$search要被搜索替换的字符串,$replace要替换搜索的字符串,$subject操作的字符串,&$count替换次数。
访函数返回在$subject搜索$search替换为$replace的字符串或数组。
1、 $search和$replace都是字符串
示例:
-
str_replace("a", "apple", "I like eat apple.");
-
str_replace("a", "apple", "I like eat apple.");
str_replace("a", "apple", "I like eat apple.");
说明:在$subject中将$search字符串替换为$replace字符串即可。
返回:"I like eapplet applepple."。
2、$search为数组
示例:
-
str_replace(array("a", "p"), "apple", "I like eat apple.");
-
str_replace(array("a", "p"), "apple", "I like eat apple.");
str_replace(array("a", "p"), "apple", "I like eat apple.");
说明:在$subject中将$search数组中的各个字符串元素依次分别替换为中$replace字符串即可。
返回:"I like eaappleapplelet aappleappleleappleapplele."。
3、$replace为数组
示例:
-
str_replace("a", array("apple", "pear"), "I like eat apple.");
-
str_replace("a", array("apple", "pear"), "I like eat apple.");
str_replace("a", array("apple", "pear"), "I like eat apple.");
说明:在$subject中将$search字符串替换为Array即可。
返回:"I like eArrayt Arraypple."。
4、$subject为数组
示例:
-
str_replace("a", "apple", array("I like eat app.", "I like eat pear."));
-
str_replace("a", "apple", array("I like eat app.", "I like eat pear."));
str_replace("a", "apple", array("I like eat app.", "I like eat pear."));
说明:返回值为数组,将$subject中的各元素分别用$search替换为$replace的值进行替换。
返回:Array ( [0] => I like eapplet applepp. [1] => I like eapplet peappler. )。
5、$search和$replace都为数组
示例:
-
str_replace(array("a", "p"), array("apple", "pear"), "I like eat apple.");
-
str_replace(array("a", "p"), array("apple", "pear"), "I like eat apple.");
str_replace(array("a", "p"), array("apple", "pear"), "I like eat apple.");
说明:在$subject中将$search的各元素值依次分别替换为$replace的各元素值。
返回:"I like eapearpearlet apearpearlepearpearle."
其中:
如果$search的数组值比$replace的数组值长,
-
str_replace(array("a", "p", "b"), array("apple", "pear"), "I like eat apple && b.");
-
str_replace(array("a", "p", "b"), array("apple", "pear"), "I like eat apple && b.");
str_replace(array("a", "p", "b"), array("apple", "pear"), "I like eat apple && b.");
将$search中多出来的数组元素在$replace中匹配的字符串替换为空串,返回"I like eapearpearlet apearpearlepearpearle && ."。
如果$replace的数组值比$search的数组值长,
-
str_replace(array("a", "p"), array("apple", "pear", "banana"), "I like eat apple && b.");
-
str_replace(array("a", "p"), array("apple", "pear", "banana"), "I like eat apple && b.");
str_replace(array("a", "p"), array("apple", "pear", "banana"), "I like eat apple && b.");
$replace中多出来的数组元素将被忽略。返回"I like eapearpearlet apearpearlepearpearle && b."。
6、$search、$replace和$subject都为数组
示例:
-
str_replace(array("a", "p"), array("apple", "pear"), array("I like eat apple.", "I like eat pear"));
-
str_replace(array("a", "p"), array("apple", "pear"), array("I like eat apple.", "I like eat pear"));
str_replace(array("a", "p"), array("apple", "pear"), array("I like eat apple.", "I like eat pear"));
说明:在$subject中将$search各元素的值依次分别替换为$replace的值,然后进行替换。
返回:Array ( [0] => I like eapearpearlet apearpearlepearpearle. [1] => I like eapearpearlet peareapearpearler )
(责任编辑:IT) |