Home>

First look at a SQL statement:

select * from table where ctime>= "[date-14]" and ctime<= "[date-1]";

I want to replace the date indicated by the square brackets in the above sql with the elements in the array below ("2015-07-01", "2015-07-15");

Use regular matching:find the first square bracket part,Replace with the first element,Then find the second one,Then replace

Use sprintf function:because the date has been calculated,Replace them in order.

Because markdown is more troublesome to write regular,Here is the picture directly

ps:Imagine,For example, there is only one time condition in SQL that needs to be replaced.Need to be modified to

$sql=sprintf ($sql, $arr [0])

To put it plainly, it would be great if the sprintf function supports an array as the second parameter.After checking it, there are indeed solutions:

The official explanation of call_user_func_array () is:

call_user_func_array — call a callback function,And take an array parameter as the parameter of the callback function

mixed call_user_func_array (callable $callback, array $param_arr)

The first parameter is called as a callback function, and the parameter array is passed as the parameter of the callback function (param_arr).

That is:the first parameter is the name of the function i want to use (sprintf above), the second parameter is the parameter of the function to be used,It's just that the parameters are passed to call_user_func_arrayok as an array, so that dynamic replacement can be achieved.

$param=$arr;
array_unshift ($param, $sql);
$sql=call_user_func_array ("sprintf", $param);

Next on str_replace — substring replacement,Array replacement

Description

mixed str_replace (mixed $search, mixed $replace, mixed $subject [, int&$count])

This function returns a string or an array.The string or array is the result of replacing all searches in the subject with replace.

If there are special replacement requirements (such as regular expressions), you should use this function to replace ereg_replace () and preg_replace ().

parameter

If search and replace are arrays, then str_replace () will do a mapping replacement of the subject.If the number of replace values ​​is less than the number of search values, the extra replacements will be performed using empty strings.If search is an array and replace is a string,Then the replacement of each element in search will always use this string.The conversion does not change the case.

If both search and replace are arrays,Their values ​​will be processed sequentially.

search

Find the target value,That is needle. An array can specify multiple targets.

replace

The replacement value for search.An array can be used to specify multiple substitutions.

subject

The array or string to perform the replacement.This is haystack.

If subject is an array,The replacement operation will traverse 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

This function returns the replaced array or string.

Version Description

5.0.0 Added count parameter.

4.3.3 Function behavior changes.There is a bug in the old version-when both the search and replace parameters are arrays,Will cause the empty search index to be skipped,But it did not move the replace internal pointer forward at the same time.The error occurs in php

4.3.3, any script that depends on this bug should first remove empty lookup values,Thus mimicking primitive behavior.

4.0.5 Most parameters can be arrays.

example

example #1 str_replace () Basic Example

<?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:you should eat pizza, beer, and ice cream every day
$phrase="you should eat fruits, vegetables, and fiber every 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 () replacement examples

<?php
//replacement order
$str="line 1 \ nline 2 \ rline 3 \ r \ nline 4 \ n";
$order=array ("\ r \ n", "\ n", "\ r");
$replace="<br />";
//replace \ r \ n characters first, so they are not converted twice
$newstr=str_replace ($order, $replace, $str);
//print f because a is replaced by b, b is replaced by c, and so on.
..
//As it is replaced from left to right,Eventually 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
//for the reasons mentioned above
$letters=array ("a", "p");
$fruit=array ("apple", "pear");
$text="a p";
$output=str_replace ($letters, $fruit, $text);
echo $output;
?>

Comment

note:This function is safe for binary objects.

caution

Understanding the replacement order

Since str_replace () is replaced from left to right,Multiple substitutions may replace previously inserted values.See examples in this document.

note:

This function is case sensitive.Use str_ireplace () for case-insensitive replacement.

php
  • Previous Mouse over the jQuery pop-up to enlarge the picture special effects
  • Next jQuery mouseover link popup follow image example code