preg_match_all(‘/ LIKE \$\d+ | LIKE \$\d+$/’, $sql_command, $valid_like_clauses) create $valid_like_clauses with following elements:
0: LIKE $1
1: LIKE $20
2: LIKE $4
after $valid_like_clauses=str_replace($key, $value, $valid_like_clauses) I expect $valid_like_clauses to be:
0: LIKE parm one \%
1: LIKE $20
2: LIKE parm two
$valid_like_clauses is a single dimentional array. When I experimented by setting $valid_like_clauses with the values that I expect from preg_match… then it str_replace works as expected. When I put the following line right before the foreach loop, the results are what I expect them to be.
That is telling $valid_like_clauses to be set to the results of str_replace (a string). My guess is you are seeing $valid_like_clauses is just the last value.
You are right, valid_like_clause remains unchanged. May be I am missing something obvious or my thinking is all wrong. The way I think my foreach should work is as follows:
With each iteration of my foreach loop, I get a new $key and $value strings. During each iteration, str_replace(…) function spins through all elements of $valid_like_clause array and replaces $key with $value if it finds them in any of the array elements and returns an updated $valid_like_clauses. The updated array is used in the next iteration of foreach with a new key and value to be replaced in all elements of the array.
So,
ITERATION ONE before str_replace is executed
$key = $1
$value = parm one \%
$value_like_value[0] = LIKE $1
$value_like_value[1] = LIKE $20
$value_like_value[2] = LIKE $4
ITERATION ONE after str_replace is executed
$key = $1
$value = parm one \%
$value_like_value[0] = LIKE parm one \%
$value_like_value[1] = LIKE $20
$value_like_value[2] = LIKE $4
ITERATION TWO before str_replace is executed
$key = $4
$value = parm two
$value_like_value[0] = LIKE parm one \%
$value_like_value[1] = LIKE $20
$value_like_value[2] = LIKE $4
ITERATION TWO after str_replace is executed
$key = $4
$value = parm two
$value_like_value[0] = LIKE parm one \%
$value_like_value[1] = LIKE $20
$value_like_value[2] = LIKE parm two
As I mentioned in my earlier post that it works exactly like I described here when I hard code $valid_like_clauses = array("LIKE $1 ", “LIKE $20”, “LIKE $4”); Its only when $valid_like_clauses array is established using preg_match … that its not working.
Yes, I tried your code but its not giving the desired result. I have posted this with the reply before yours, but I will post it for as well.
The way I think my foreach should work is as follows:
With each iteration of my foreach loop, I get a new $key and $value strings. During each iteration, str_replace(…) function spins through all elements of $valid_like_clause array and replaces $key with $value if it finds them in any of the array elements and returns an updated $valid_like_clauses. The updated array is used in the next iteration of foreach with a new key and value to be replaced in all elements of the array.
So,
ITERATION ONE before str_replace is executed
$key = $1
$value = parm one \%
$value_like_value[0] = LIKE $1
$value_like_value[1] = LIKE $20
$value_like_value[2] = LIKE $4
ITERATION ONE after str_replace is executed
$key = $1
$value = parm one \%
$value_like_value[0] = LIKE parm one \%
$value_like_value[1] = LIKE $20
$value_like_value[2] = LIKE $4
ITERATION TWO before str_replace is executed
$key = $4
$value = parm two
$value_like_value[0] = LIKE parm one \%
$value_like_value[1] = LIKE $20
$value_like_value[2] = LIKE $4
ITERATION TWO after str_replace is executed
$key = $4
$value = parm two
$value_like_value[0] = LIKE parm one \%
$value_like_value[1] = LIKE $20
$value_like_value[2] = LIKE parm two
As I mentioned in my earlier post that it works exactly like I described here when I hard code $valid_like_clauses = array("LIKE $1 ", “LIKE $20”, “LIKE $4”); Its only when $valid_like_clauses array is established using preg_match … that its not working.
I was treating the multi-dimensional array returned by preg_match… as a single dimensional array. Even though you had mentioned it in your earlier post, your code made me see it.