This article describes how to use arrays to replace matching items in strings in PHP. For more information, see the following SQL statement:
select * from table where ctime >= '[date-14]' and ctime <= '[date-1]';
You want to replace the dates represented by braces in the preceding SQL statement with the element array ('2017-07-01 ', '2017-07-15') in the following array ');
Regular Expression matching: Find the first bracket, replace it with the first element, find the second element, and then replace it.
Use the sprintf function: because the date has been calculated, it can be replaced in order.
Because markdown is difficult to write regular expressions, we can directly split it here.
Ps: imagine that there is only one time condition to replace in SQL, and it needs to be modified
$sql = sprintf($sql,$arr[0])
To put it bluntly, it would be nice if the sprintf function supports the second parameter as an array. After checking the information, you can find a solution:
The official explanation of call_user_func_array () is:
Call_user_func_array-call the callback function and use an array parameter as the callback function parameter.
mixed call_user_func_array ( callable $callback , array $param_arr )
Call the first parameter as the callback function, and pass in the parameter array as the parameter (param_arr) of the callback function.
That is to say, the first parameter is the name of the function you want to use (sprintf in the above section), the second parameter is the parameter of the function to be used, but the parameter is passed to call_user_func_arrayok in an array, in this way, dynamic replacement can be implemented.
$param = $arr;array_unshift($param,$sql);$sql = call_user_func_array('sprintf',$param);
Next we will introduce str_replace-replace the substring and replace the array.
Description
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.
If you have special replacement requirements (such as regular expressions), you should use this function to replace ereg_replace () and preg_replace ().
Parameters
If search and replace are arrays, str_replace () will replace the ing between the two in subject. If the number of replace values is less than the number of search values, null strings are used for redundant replace. If search is an array and replace is a string, the replacement of each element in search always uses this string. This conversion will not change the case sensitivity.
If both search and replace are arrays, their values are processed in sequence.
Search
The target value, that is, needle. Multiple targets can be specified in an array.
Replace
The replacement value of search. An array can be used to specify multiple replicas.
Subject
Execute the replaced array or string. That is, haystack.
If the subject is an array, the replacement operation traverses the entire subject, and the return value is also an array.
Count
Note: If specified, it controls the number of matching and replacement times.
Return Value
This function returns the replaced array or string.
Version description
5.0.0 adds the count parameter.
4.3.3 function behavior changes. In the old version, there is a BUG. When both search and replace parameters are arrays, empty search indexes will be skipped, but the replace internal pointer is not moved forward at the same time. This error occurs in PHP
4.3.3 Any script dependent on this BUG should first remove the null search value to simulate the original behavior.
4.0.5 most parameters can be an array.
Example
Example #1 basic Example of str_replace ()
<? Php // Value assignment:$ Bodytag = str_replace ("% body %", "black ",""); // Value assignment: Hll Wrld f PHP $ vowels = array (" a "," e "," I "," o "," u "," ", "E", "I", "O", "U"); $ onlyconsonants = str_replace ($ vowels, "", "Hello World of PHP"); // Value assignment: you shoshould eat pizza, beer, and ice cream every day $ phrase = "You shoshould eat fruits, vegetables, and fiber every day. "; $ healthy = array (" fruits "," vegetables "," fiber "); $ yummy = array (" pizza "," beer "," ice cream "); $ newphrase = str_replace ( $ Healthy, $ yummy, $ phrase); // value: 2 $ str = str_replace ("ll", "", "good golly miss molly! ", $ Count); echo $ count;?>
Example #2 possible str_replace () Replacement examples
<? Php // replacement sequence $ str = "Line 1 \ nLine 2 \ rLine 3 \ r \ nLine 4 \ n"; $ order = array ("\ r \ n ", "\ n", "\ r"); $ replace ='
'; // Replace \ r \ n characters first, so they are not converted twice $ newstr = str_replace ($ order, $ replace, $ str); // Output F, because A is replaced by B, B is replaced by C, and so on... // because E is replaced from left to right, F is replaced by $ search = array ('A', 'B', 'C', 'D', 'E '); $ replace = array ('B', 'C', 'D', 'E', 'F'); $ subject = 'a'; echo str_replace ($ search, $ replace, $ subject); // output: apearpearle pear // For the reason mentioned above $ letters = array ('A', 'P '); $ fruit = array ('apple', 'pear '); $ text = 'a P'; $ output = s Tr_replace ($ letters, $ fruit, $ text); echo $ output;?>
Note
Note: this function can be safely used for binary objects.
Caution
Understanding the order of replacement
Because the replacement of str_replace () is performed from left to right, the previously inserted values may be replaced when multiple replacement is performed. See the example in this document.
Note:
This function is case sensitive. Use str_ireplace () for case-insensitive replacement.