-1

Possible Duplicate:
Converting ereg expressions to preg

I'm trying to fix some old code so that it works with PHP 5.3. Hopefully the first conversion to str_replace is okay but I'm completely unsure about the second conversion to preg_replace.

Any help would be very much appreciated. Thanks

$this->Query = str_replace(" where "," WHERE ", $this->Query);
$this->Query = str_replace(" select "," SELECT ", $this->Query);
$this->Query = str_replace(" from "," FROM ", $this->Query);
$this->Query = str_replace(" as "," AS ", $this->Query);

// $this->Query = eregi_replace(" WHERE ", " where ", $this->Query);
// $this->Query = eregi_replace("SELECT ", "select ", $this->Query);
// $this->Query = eregi_replace(" FROM ", " from ", $this->Query);
// $this->Query = eregi_replace(" AS ", " as ", $this->Query);

$TempQuery = eregi_replace("^select .* from ", "select count(1) from ", $this->Query);
$TempQuery = eregi_replace(" order by .*$", "", $TempQuery);
Community
  • 1
  • 1
Jason
  • 607
  • 3
  • 9
  • 25
  • 7
    you do know that SQL doesn't have to be upper-cased? (and you did the exact opposite of what eregi_replace was doing) – yoavmatchulsky Nov 15 '11 at 10:45
  • Refer to the PCRE syntax at http://php.net/manual/en/reference.pcre.pattern.syntax.php – BoltClock Nov 15 '11 at 10:46
  • You can do that with, `preg_replace('/pattern_here/i', 'replacement', $subject)`, but why go through all this hassle? The queries will work either way. – Shef Nov 15 '11 at 10:48
  • I was just keeping the code the same and as it had a the comment // retouch query to avoid case matching problems – Jason Nov 15 '11 at 10:59

2 Answers2

1

*eregi_replace* must be replace by *preg_replace*

ex :

$string = $this->Query;
$pattern = '/^select .* from /i';
$replacement = "select count(1) from ";
$TempQuery = preg_replace($pattern, $replacement, $string);
TeChn4K
  • 2,317
  • 2
  • 21
  • 23
1

As said, SQL syntax doesn't have to be in upper-case... However to answer your question anyway:

$TempQuery = preg_replace("/^select .*? from /i", "select count(1) from ", $this->Query);
$TempQuery = preg_replace("/ order by .*?$/i", "", $TempQuery);

That should work. I made the regex case-insensitive since you seem to have trouble with upper- and lower-case. Also, I made the .* ungreedy by adding a question mark to avoid extra complications.

Tim S.
  • 13,597
  • 7
  • 46
  • 72