Samutz
Samutz

Reputation: 2300

simple preg_replace() that I can't get working (i suck at regex)

$count_sql = preg_replace("/SELECT(.+?)FROM/", "SELECT COUNT(*) FROM", $sql);

It's probably pretty obvious what I'm trying to do, but I am terrible with regex.

I need to replace anything between SELECT and FROM with COUNT(*).

Tried using (.+), (.+?), (.*), and (.*?).

Upvotes: 0

Views: 318

Answers (1)

Greg
Greg

Reputation: 321688

It looks OK... does your SQL contain newlines? If so, you'll need the s modifier:

$count_sql = preg_replace("/SELECT(.+?)FROM/s", "SELECT COUNT(*) FROM", $sql);

Upvotes: 2

Related Questions