#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    28
    Rep Power
    0

    How to use preg_replace instead of eregi php function


    Hello,

    When I moved to a new server, eregi function generates too many errors. I like to use preg_replace instead of eregi function.

    Here is my block of code:
    if($rstate && !$rcity && !eregi("outside",$rstate)){
    $h1title=str_replace('DDD',$tsstate,str_replace('XXX',$tsstate,$pagetitlet));
    echo str_replace('DDD',$tsstate,str_replace('XXX',$tsstate,$pagetitlet));

    I changed it to:
    if($rstate && !$rcity && !preg_replace("outside",$rstate)){
    $h1title=str_replace('DDD',$tsstate,str_replace('XXX',$tsstate,$pagetitlet));
    echo str_replace('DDD',$tsstate,str_replace('XXX',$tsstate,$pagetitlet));

    But it is looking for another value. How do I add the third parameter for preg_replace to get it worked?

    Thanks.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    I think there are several misunderstandings.

    First of all, eregi() doesn't replace anything. It simply tries to match a string to a regex. The modern equivalent of this would be preg_match() with the "i" modifier. The manual tells you this, actually.

    However, the regex is completely useless anyway, because you simply wanna search for a substring. What you want is stripos().
    Last edited by Jacques1; June 21st, 2013 at 09:49 AM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    28
    Rep Power
    0
    You mean I just need to replace eregi with stripos function and it will work?

    Thanks.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Basically, yes. But you cannot literally replace it, because the order of arguments is different, and you need a strict check for false, because stripos() returns 0 if the string starts with "outside".

    PHP Code:
    stripos($rstate"outside") === false 
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo