0

I need to replace string between first SELECT and first FROM, but still not working if I try this :

Here is my SQL string to replace :

$strSQLreport = "SELECT ... FROM ... WHERE ... AND volu_id IN (SELECT FK_volu_id FROM)"

Here is my preg_replace :

preg_replace( "/SELECT(.*)FROM/s", "SELECT volu_id AS id FROM", $strSQLreport, 1 )

It takes the second from, and I want only replace before de first FROM! Thanks for help!

Michael Berkowski
  • 260,803
  • 45
  • 432
  • 377
Elmux
  • 399
  • 1
  • 4
  • 14
  • 5
    The point you wonder about is called greediness / laziness. Add a `?` after the `*`: `(.*?)`. I suggest you start to read about regular expressions first, e.g. http://www.regular-expressions.info/repeat.html – hakre Mar 12 '12 at 13:32
  • Maybe this helps `/^SELECT(.*?)FROM/s` – Wh1T3h4Ck5 Mar 12 '12 at 13:33
  • oh, hakre was quicker :) – Wh1T3h4Ck5 Mar 12 '12 at 13:34
  • Sorry to get off topic, curious why you need to do this? – Luc Mar 12 '12 at 13:35
  • possible duplicate of [PHP preg_replace stripping](http://stackoverflow.com/questions/9118837/php-preg-replace-stripping) (one of the may, the question is quite common) - and there is more to read: [How does accepting an answer work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) ;) – hakre Mar 12 '12 at 13:38
  • @Luc, is simply for no replication. I don't want to call my function an other time, because the string is very very long :) Thanks for the answer hakre and Wh1T3h4Ck5. It works like a charm!! – Elmux Mar 12 '12 at 14:00

0 Answers0