Hi,
I need some help on how to write some regular expressions for the following scenario:
I have a function in which I would like to pass an sql query, for example:
$q = "select t.* from table t where 1"
and I would like to replace "t.*" with "1", effectively giving me:
"select 1 from table t where 1"
Now there can be multiple fields after the select, so I came up with the following:
$q = preg_replace("/(select).+(from)/si", "$1 1 $2", $q);
which worked just fine, until I ran into some more complex queries:
If I have a union with a secondary select, or a subquery with a select, it would replace everything between the FIRST occurrence of "select" and the LAST occurrence of "from".
For example, if I had
$q = "(select t. from table t ) union (select t2. from table2 t2)"
I would get:
"(select 1 from table2 t2)";
However, what i would like is $q="(select 1 from table t) union (select 1 from table2 t2)"
Can anyone point me in the right direction as to how I can accomplish that?
Thanks