1

i want to find first select ... from and replace that only, following code replace all select..from in sql query, i just need for first select..from

preg_replace('#select(.*?)from#is', "select count($expr) as counted from", $sql);
Basit
  • 16,316
  • 31
  • 93
  • 154

1 Answers1

1

There is a fourth optional parameter called 'limit' for preg_replace() which defines the number of replacements, and which defaults to -1 (meaning no limit)

preg_replace('#select(.*?)from#is', "select count($expr) as counted from", $sql, 1);
pavium
  • 14,808
  • 4
  • 33
  • 50