Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts

    changing eregi to preg_match

    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.
    PHP Code:
    if(!eregi("^select",$sql)){    
                  echo 
    "Wrong Query<hr>$sql<p>";
                        return 
    false;        } 
    and this is my attempt at fixing it.

    PHP Code:
    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?
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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?

    Edit:
    BTW, I'm thinking you can probably get away with just an stripos instead:
    PHP Code:
    if (=== 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.
    Last edited by Fou-Lu; 02-17-2010 at 05:33 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Try
    Code:
    /^\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
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by JAY6390 View Post
    Try
    Code:
    /^\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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #6
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    fou there's different functions for each query type.

    so...

    PHP Code:
    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
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #7
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Nothing wrong with the regex

    PHP Code:
    $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

    PHP Code:
    echo "Wrong Query<hr>[$sql]<p>"
    HTH,

    Dai


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •