Let's take a look at the SQL statement:
SELECT * FROM table where CTime >= ' [date-14] ' and CTime <= ' [date-1] ';
To convert the date in parentheses in the above SQL to the element array in the following array (' 2015-07-01 ', ' 2015-07-15 ');
With a regular match: Find the first bracket section, replace with the first element, then find the second one, and then replace the
With the sprintf function: Because the date has already been calculated, it is OK to replace it in order.
Because markdown writing is more troublesome, here is directly on the picture
PS: Imagine that if there is only one time condition in SQL that needs to be replaced, it needs to be modified
$sql = sprintf ($sql, $arr [0])
Well, frankly, it would be nice if the sprintf function supports the second argument as an array. After checking it out, there can be a solution:
Call_user_func_array () The official explanation is:
Call_user_func_array-calls the callback function and takes an array argument as a parameter to the callback function
Mixed Call_user_func_array (callable $callback, array $param _arr)
The first parameter is invoked as a callback function (callback), and the parameter array is passed (Param_arr) as a parameter of the callback function.
That is, the first parameter is the name of the function you want to use (sprintf above), and the second parameter is the argument that will use the function, except that the parameter is passed to the Call_user_func_arrayok as an array, so that the dynamic substitution is possible.
$param = $arr;
Array_unshift ($param, $sql);
$sql = Call_user_func_array (' sprintf ', $param);
Next, we'll introduce str_replace-substring substitution, array substitution
Description
Mixed Str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])
The function returns a string or array. The string or array is the result of replacing all search in subject with replace.
If you have some special replacement requirements (such as regular expressions), you should replace ereg_replace () and Preg_replace () with this function.
Parameters
If search and replace are arrays, then str_replace () will replace the mappings for subject. If the value of replace is less than the number of search, the extra substitution is done with an empty string. If search is an array and replace is a string, the substitution of each element in search will always use this string. The conversion does not change case.
If both search and replace are arrays, their values are processed sequentially.
Search
The target value of the lookup, which is needle. An array can specify multiple destinations.
Replace
The replacement value for search. An array can be used to specify multiple substitutions.
Subject
An array or string that performs the substitution. which is haystack.
If subject is an array, the substitution operation traverses the entire subject and the return value will be an array.
Count
Note: If specified, it will control the number of matches and replacements.
return value
The function returns the substituted array or string.
Version description
5.0.0 the new count parameter.
4.3.3 function behavior changes. There is a bug--in the old version when both search and replace two parameters are arrays, the empty search index is skipped, but the replace internal pointer is not moved forward at the same time. This error occurs in PHP
4.3.3, any script that relies on this BUG should first remove the empty lookup value to simulate the original behavior.
4.0.5 most parameters can be arrays.
Example
Example #1 str_replace () basic paradigm
<?php
//assignment: <body text= ' black ' >
$bodytag = Str_replace ("%body%", "Black", "<body text= '%body%") > ");
Assignment: Hll wrld f PHP
$vowels = Array ("A", "E", "I", "O", "U", "a", "E", "I", "O", "U");
$onlyconsonants = Str_replace ($vowels, "", "Hello World of PHP");
Assignment: Should eat pizza, beer, and ice cream every day $phrase = "For you should eat, fruits
, and vegetables Eve Ry Day. ";
$healthy = Array ("Fruits", "vegetables", "fiber");
$yummy = Array ("Pizza", "beer", "ice Cream");
$newphrase = Str_replace ($healthy, $yummy, $phrase);
Assignment: 2
$str = Str_replace ("ll", "" "," Good Golly Miss Molly! ", $count);
echo $count;
? >
Example #2 possible str_replace () substitution example
<?php
//substitution order
$str = "line 1\nline 2\rline 3\r\nline 4\n";
$order = Array ("\ r \ n", "\ n", "\ R");
$replace = ' <br/> ';
Replace the \ r \ n characters first, so they are not converted to two times
$newstr = Str_replace ($order, $replace, $str);
Output F, because A is replaced by B, B is replaced by C,
etc. ... Because of substitution from left to right, the final E is replaced by F
$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
//Because of the reasons mentioned above
$letters = Array (' A ', ' P ');
$fruit = Array (' Apple ', ' pear ');
$text = ' A P ';
$output = Str_replace ($letters, $fruit, $text);
echo $output;
? >
Comments
Note: This function can be used safely for binary objects.
Caution
Understanding the substitution order
Because Str_replace () is replaced from left to right, multiple substitutions may replace the values that were inserted before. See examples of this document.
Note:
The
function is case-sensitive. Use Str_ireplace () to make case-insensitive substitutions.