Function prototypes: Mixed str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])
The three parameter types of this function are mixed, so they can both be strings or arrays, so there are several uses:
1, $search and $replace are strings, which is the most common usage
Echo Str_replace ("A", "Apple", "This is a demo.");
Output: This is Apple demo.
2, $search An array, the elements in $search are replaced by $replace, and the final output is a string.
echo str_replace (Array ("A", "P", "O"), "Apple", "This is a demo.");
Output: This is a apple Apple le demApple.
3, $replace an array, will be error and will not output the expected results, do not recommend the use of
Echo Str_replace ("A", array ("Apple", "pear"), "This is a demo.");
Output: This is Array demo. and error Notice:array to string conversion
4, $subject an array, each element in the $subject is replaced by an independent one, and the final output is a group.
Print_r (Str_replace ("A", "Apple", Array ("This is a demo.", "the" This is not a demo. ")));
Output: Array ([0] = = Apple demo. [1] = = This isn't Apple demo. )
5, $search and $replace are an array, then can be divided into three kinds of situations:
a). $search as long as the $replace, the same subscript corresponds to the replacement
b). $search longer than $replace, the array elements that are more than $replace in $search are replaced with empty strings
c). $search shorter than $replace, the array elements in the $replace are ignored
echo str_replace (Array ("A", "O"), Array ("Apple", "pear"), "This is a demo.");
Output: This is apple dempear.
6, $search, $replace and $subject are all arrays, which is a combination of the above
Print_r (Str_replace (Array ("A", "O"), Array ("Apple", "pear"), Array ("This is a demo.", "Here is a demo."));
Output: Array ([0] = = Apple dempear. [1] = = This is npeart Apple dempear. )