Describes four usage of the PHP string replacement str_replace () function: str_replace
The following describes how to replace str_replace () with a PHP string:
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
This function returns a string or array. This string or array is the result after all the searches in the subject are replaced by replace.
1. $ search, the string to be replaced, or an array
2. $ replace, a string or array to be replaced
3. $ subject: the queried string or Array
4. $ count (Optional). If specified, it is set to the number of replicas.
5. Return Value: This function returns the replaced array or string (new)
<? Php // instance 1: String replacement string $ str1 = str_replace ("red", "black", "red green yellow pink purple"); echo $ str1 .""; // The output result is black green yellow pink purple?> <? Php // Example 2: String replacement array key value $ arr = array ("blue", "red", "green", "yellow "); $ str1 = str_replace ("red", "pink", $ arr, $ I); print_r ($ str1);?> <? Php // Example 3: array replacement array, ing replacement $ arr1 = array ("banana", "orange"); $ arr2 = array ("pitaya", "tomato "); $ con_arr = array ("apple", "orange", "banana", "grape"); $ con_rep = str_replace ($ arr1, $ arr2, $ con_arr, $ count ); print_r ($ con_rep);?> <? Php // Example 4: If $ search is an array and $ replace is a string, $ search = array ("banana", "grape"); $ replace = "tomato "; $ arr = array ("banana", "apple", "orange", "grape"); $ new_arr = str_replace ($ search, $ replace, $ arr, $ count ); print_r ($ new_arr);?>
Summary
The above section describes four usage of the PHP string replacement str_replace () function. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!