...

View Full Version : changing eregi to preg_match



hinch
02-17-2010, 06:17 PM
I have several eregi comparisons in a mysql class which are throwing up warnings now on 5.3 of php (since eregi is deprecated)
I'm trying to change them to preg_match instead but failing.

this is the offending code.

if(!eregi("^select",$sql)){
echo "Wrong Query<hr>$sql<p>";
return false; }

and this is my attempt at fixing it.


if(!preg_match("/^select/i",$sql)){
echo "Wrong Query<hr>$sql<p>";
return false; }

however its always hitting the wrong query clause where am i going wrong?

Fou-Lu
02-17-2010, 06:29 PM
Hmm, that looks right to me. Start (^) with 'select', no end bound.
Daft question, are you sure you're trying it with a select and not an update or something different?


BTW, I'm thinking you can probably get away with just an stripos instead:


if (0 === stripos($sql, 'select'))
{
print 'Query is good';
}

You'd want to keep the same criteria and flip it all with a ! check to see if it failed. This needs to use === since the function will return false if nothing is matched, but 0 if it matches the first position. Best I know, $needle can be a string in PHP.

JAY6390
02-17-2010, 06:29 PM
Try

/^\s*?select/i
It might be that you had a space in your query text. If you can echo your text for your sql that would be useful

Fou-Lu
02-17-2010, 06:34 PM
Try

/^\s*?select/i
It might be that you had a space in your query text. If you can echo your text for your sql that would be useful

Added to this one, surround you're string with '' marks so you can see if a space is in there.
I was thinking a spacing issue too, but then I would presume that the ereg would have struggled as well.

JAY6390
02-17-2010, 06:37 PM
Yeah, it was a long shot :)
I'm pretty sure eregi would have worked the same, although I've always been told preg not eregi from the first time I wrote a regex

hinch
02-17-2010, 06:39 PM
fou there's different functions for each query type.

so...


if(!eregi("^select",$sql)){
if(!eregi("^insert",$sql)){
if(!eregi("^update",$sql)){

I didn't write this class originally someone else did I've just recycled it over the years since it just worked :)

there's no spaces in the query string before the select because its running a "boxed" ecomm product thats always worked fine using the old system its only this new host thats obviously newer version of php so i need to convert to preg_match to stop the warnings.

will try /^\s*?select/i and let you know though its got me confused totally I'm not exactly good with regex's to start with

DaiWelsh
02-18-2010, 02:47 PM
Nothing wrong with the regex



$sql = 'SELECT blah from blah where blah';
if(!preg_match("/^select/i",$sql)){
echo "Wrong Query<hr>$sql<p>";
} else {
echo "Right Query";
}


http://www.metatest.co.uk/test_ereg.php
what do you get output as the value of $sql when it says wrong query (I always wrap debug vars in [] to see whitespace issues so maybe try


echo "Wrong Query<hr>[$sql]<p>";

HTH,

Dai



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum