How to fix ‘Function eregi() is deprecated’ in PHP 5.3.0?

I used to use eregi for validating email address input that matches to the regular expression.

1
2
3
4
5
6
if(!eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $str)) {
    $msg = 'email is not valid';
}
else {
$valid = true;
}
if(!eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $str)) {
	$msg = 'email is not valid';
}
else {
$valid = true;
}

That would return true if given email address is matches to username@domain.ext pattern. Unfortunately, after upgrading PHP to the earlier version (5.3.0), it wont work properly. This is because eregi is one of several functions that are deprecated in the new version of PHP.

Solution how to fix Function Deprecated:

Use preg_match with the ‘i’ modifier instead. i means that regular expression is case insensitive. So the code become like this:

1
2
3
4
5
6
if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $str)) {
    $msg = 'email is not valid';
}
else {
$valid = true;
}
if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $str)) {
	$msg = 'email is not valid';
}
else {
$valid = true;
}

The list of functions that are deprecated in PHP 5.3.0:

  • call_user_method() (use call_user_func() instead)
  • call_user_method_array() (use call_user_func_array() instead)
  • define_syslog_variables()
  • dl()
  • ereg() (use preg_match() instead)
  • ereg_replace() (use preg_replace() instead)
  • eregi() (use preg_match() with the ‘i’ modifier instead)
  • eregi_replace() (use preg_replace() with the ‘i’ modifier instead)
  • set_magic_quotes_runtime() and its alias, magic_quotes_runtime()
  • session_register() (use the $_SESSION superglobal instead)
  • session_unregister() (use the $_SESSION superglobal instead)
  • session_is_registered() (use the $_SESSION superglobal instead)
  • set_socket_blocking() (use stream_set_blocking() instead)
  • split() (use preg_split() instead)
  • spliti() (use preg_split() with the ‘i’ modifier instead)
  • sql_regcase()
  • mysql_db_query() (use mysql_select_db() and mysql_query() instead)
  • mysql_escape_string() (use mysql_real_escape_string() instead)
  • Passing locale category names as strings is now deprecated. Use the LC_* family of constants instead.
  • The is_dst parameter to mktime(). Use the new timezone handling functions instead.

Ref:

http://php.net/manual/en/migration53.deprecated.php

  • http://www.facebook.com/profile.php?id=1382860573 Baze Petreski

    my problem is if (eregi( $allowable, $img_file ) ) {
    $image_info = @getimagesize( COM_MEDIA_BASE .”/”.$listdir.’/’.$img_file);
    $file_details['file'] = COM_MEDIA_BASE . $listdir.”/”.$img_file;
    $file_details['img_info'] = $image_info;
    $file_details['size'] = filesize( COM_MEDIA_BASE .$listdir.”/”.$img_file);
    $images[$entry] = $file_details;
    } else {
    // file is document
    $file_details['size'] = filesize( COM_MEDIA_BASE .$listdir.”/”.$img_file);
    $file_details['file'] = COM_MEDIA_BASE .$listdir.”~/~i”.$img_file;
    $docs[$entry] = $file_details;

    • takien


      if (preg_match("/$allowable/i", $img_file ) ) {

  • Turtleman

    seems like allot of people have the same issued

    if (eregi(“.inc.php”,$HTTP_SERVER_VARS['PHP_SELF']) || eregi(“.inc.php”,$_SERVER['PHP_SELF'])) {
        echo “rnrnForbidden 403rnrnForbidden 403rnThe document you are requesting is forbidden.rnrn”;
        exit;
    }

    any help please?

    • takien

       
      if (preg_match("/.inc.php/i",$HTTP_SERVER_VARS['PHP_SELF']) || preg_match("/.inc.php/i",$_SERVER['PHP_SELF'])) {
         
      echo "rnrnForbidden
      403rnrnForbidden 403rnThe
      document you are requesting is forbidden.rnrn";
          exit;
      }

  • Dfgh

    tryuty

  • Jj_trasher

    How would I re-write this? Thanks in advance :)

    $file = basename(__FILE__);
    if(preg_match($file,$_SERVER['REQUEST_URI'])) {
        die(“You don’t have permissions to access this file.”);
    }

    • takien

       
      $file = basename(__FILE__);
      if(preg_match("/".preg_quote($file)."/i",$_SERVER['REQUEST_URI'])) {
          die("You don't have permissions to access this file.");
      }

      here you need preg_quote becaouse $file may contains special characters like dot and or slash

      • Karthiksankaran Soft

        How can i re-write this?
        if (!eregi(“^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$”, $_POST["co_email"]))  { $myError = ” . $invalidEmail . ”;      }

        • takien

           Here we go

          if (!preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i", $_REQUEST["co_email"]) {
               $myError = '' . $invalidEmail . '';  
          }

          • Karthiksankaran Soft

             wow great.Thank you takien.now working fine.

  • Ggetiel

    can someone tell me what’s wrong with this code accept Egeri.
    how can i make it work.
    Gerarf

  • http://www.r-gate.net/ Mohamed Tair

    Thanks ^^

  • Be8

    After changimt to preg_match i dont get any more errors but now nothing writes in in my email.xml file as it suppose to :( can you help me pls?  

     if(trim($_POST['email']) == ”)  {
            $hasError = true;
        } else if (!eregi(“^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$”, trim($_POST['email']))) {
            $hasError = true;
        } else {
            $email = trim($_POST['email']);

  • http://profile.yahoo.com/JSN7H6U4XXQTMJUO76TZGVF77Q Alex Brian Kux

    Idiots.. kiddies. make a whole world update hundreds of applications

    • JMR

      Absolutely right, Alex. This guys must think we are kids playing their game.

  • Rgolias2

    if(
          eregi(“[a-z0-9_.-]“, $querystring[0]) &&
            $querystring[0]!=”index” &&
            is_file ($querystring[0] . “.phtml”) &&
            $querystring[0] . “.php” “!= basename (__FILE__)
          )

  • Lahtis

    elseif(preg_match(“/^show_news.php/”, $PHP_SELF)){

    is this correct??

  • Peter

    thanks alot.
    Peter

  • Jyotikanta

    thanks a lot.

  • Hoangnd1988

    Anyone help me change to pre_match()?

  • Hoangnd1988
  • Mohsen

    Thanks man:)
    My problem solved.

  • Manuel00

    thz excelent post

  • Gunn ‘em Down

    thanx man

  • linuxadmin

    It’s works, Thank you

    • takien

      you’re welcome

  • mike

    how would i rewrite this?: if((eregi($suspect, $lowmsg)) || (eregi($suspect, strtolower($_POST['name']))) || (eregi($suspect, strtolower($_POST['email']))))

  • Bram

    Hi, how can i change this? iam kinda stuck here…thx

    function isValidEmail($email){ $pattern = “^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$”; if (eregi($pattern, $email)){ return true; } else { return false; } } function processForm(){ if (isset($_POST['submitBtn'])){ $email = isset($_POST['email']) ? trim($_POST['email']) : ”; $name = isset($_POST['name']) ? trim($_POST['name']) : ”; $message = isset($_POST['message']) ? trim($_POST['message']) : ”; if (!$this->isValidEmail($email)) $this->errorList[] = “Invalid email address!”;

  • Bram

    again..

    function isValidEmail($email){
    $pattern = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$";

    if (eregi($pattern, $email)){
    return true;
    }
    else {
    return false;
    }
    }

    function processForm(){
    if (isset($_POST['submitBtn'])){
    $email = isset($_POST['email']) ? trim($_POST['email']) : '';
    $name = isset($_POST['name']) ? trim($_POST['name']) : '';
    $message = isset($_POST['message']) ? trim($_POST['message']) : '';

    if (!$this->isValidEmail($email)) $this->errorList[] = "Invalid email address!";

  • B

    How can i change this? pls help…


    function isValidEmail($email){
    $pattern = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$";

    if (eregi($pattern, $email)){
    return true;
    }
    else {
    return false;
    }
    }

    function processForm(){
    if (isset($_POST['submitBtn'])){
    $email = isset($_POST['email']) ? trim($_POST['email']) : '';
    $name = isset($_POST['name']) ? trim($_POST['name']) : '';
    $message = isset($_POST['message']) ? trim($_POST['message']) : '';

    if (!$this->isValidEmail($email)) $this->errorList[] = "Invalid email address!";

  • Renato Tarso

    Thanks…. :)

  • dd

    goood

  • SAM

    please help me !! i add a contact form in my website but when i submit the form it says this
    Deprecated: Function eregi() is deprecated in/home/nodi/public_html/contact/include/fgcontactform.phpon line 548

    Deprecated: Function split() is deprecated in/home/nodi/public_html/contact/include/class.phpmailer.phpon line 470

  • sadam

    tanx,,, mly error is finish by using

    preg_match() function….

  • JRDH

    Can somebody please tell me how to change this code. Simply replacing eregi with
    preg_match doesn’t work!

    May thanks in advance!

    // paginas aanroepen
    $map = ‘pages’;
    $ext = ‘.php’;
    if(isset($_GET['pagina']) AND $_GET['pagina'] != ‘home’){
    if(eregi(‘http://’,$_GET['pagina'])){
    include $map.’/error.php’;
    }
    elseif(file_exists($map.’/’.$_GET['pagina'].$ext)){
    include $map.’/’.$_GET['pagina'].$ext;
    }else{
    include $map.’/error.php’;
    }
    }else{
    include $map.’/home.php’;
    }
    ?>

  • Jamie

    Hey, how would I recode this please, it’s driving me insane and I’m a pretty basic PHP user. :(

    if (email_is_valid($youremail) && !eregi(“r”,$youremail) && !eregi(“n”,$youremail) && $yourname != “” && $yourmessage != “” && substr(md5($user_answer),5,10) === $answer) { mail($to,$subject,$message,$headers);
    Regards,
    Jamie

  • Mark

    Really having trouble with this how would i rewrite this
    if (eregi(trim($id),$_SERVER['HTTP_USER_AGENT'])) {
    redirect(‘mobile’);

  • dileep

    ) Deprecated: Function eregi() is deprecated in C:wampwwwauctionproduct_libclassbrowser.php on

    how to fix this error help me

    if (eregi(“win”, $agent))
    $bd['platform'] = “Windows”;
    elseif (eregi(“mac”, $agent))
    $bd['platform'] = “MacIntosh”;
    elseif (eregi(“linux”, $agent))
    $bd['platform'] = “Linux”;
    elseif (eregi(“OS/2″, $agent))
    $bd['platform'] = “OS/2″;
    elseif (eregi(“BeOS”, $agent))
    $bd['platform'] = “BeOS”;

  • Don

    Fixed with preg_match. Thank you very much.

  • alif

    its solve the prob, thanks a lot :)

  • bebekid

    Hello ,

    My problem is :

    if (($logs_file != “.”) && ($logs_file != “..”) && (eregi (SLOGIN_LOG_SUFFIX . “.txt$”, strtolower ($logs_file))) && (eregi (“^” . SLOGIN_LOG_PREFIX, strtolower ($logs_file)))) {

    $logsize = ceil (filesize ($logs_file) / 1024);

    $logs_file = strtolower ($logs_file);

    $logs_file = substr ($logs_file, strlen (SLOGIN_LOG_PREFIX), strlen ($logs_file));

    $logs_file = substr ($logs_file, 0, strlen (fslogin_logprefix ()));

    echo “”;

    echo “$logs_file ($logsize k)”;

    echo “n”;

    $logs_count++;

    How do i write the code with preg_match

  • Ivica

    my problem is code in php nuke:

    foreach ($_POST as $secvalue) {
    if ((eregi(“]*iframe*”?[^>]*”, $secvalue)) ||
    (eregi(“]*object*”?[^>]*”, $secvalue)) ||
    (eregi(“]*applet*”?[^>]*”, $secvalue)) ||
    (eregi(“]*meta*”?[^>]*”, $secvalue)) ||
    (eregi(“]*onmouseover*”?[^>]*”, $secvalue)) ||
    (eregi(“]script*”?[^>]*”, $secvalue)) ||
    (eregi(“]*body*”?[^>]*”, $secvalue)) ||
    (eregi(“]style*”?[^>]*”, $secvalue))) {
    die ($htmltags);

    and this is error in page:
    Warning: eregi() expects parameter 2 to be string, array given in /home/rasani/public_html/mainfile.php on line 161

    Warning: eregi() expects parameter 2 to be string, array given in /home/rasani/public_html/mainfile.php on line 162

    Warning: eregi() expects parameter 2 to be string, array given in /home/rasani/public_html/mainfile.php on line 163

    Warning: eregi() expects parameter 2 to be string, array given in /home/rasani/public_html/mainfile.php on line 164

    Warning: eregi() expects parameter 2 to be string, array given in /home/rasani/public_html/mainfile.php on line 165

    Warning: eregi() expects parameter 2 to be string, array given in /home/rasani/public_html/mainfile.php on line 166

    Warning: eregi() expects parameter 2 to be string, array given in /home/rasani/public_html/mainfile.php on line 167

    Warning: eregi() expects parameter 2 to be string, array given in /home/rasani/public_html/mainfile.php on line 168

  • Occupied_at_work

    Any help would be appreciated, I’m lost and not sure how to re-write the following

    if(ereg($reg_exp,$input_value))
    {
    return false;
    }
    return true;
    }
    function validate_email($email)
    {
    return eregi(“^[_.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+.)+[a-zA-Z]{2,6}$”, $email);
    }

  • http://www.srinitai.net Vinay Shankar

    Someone Help!!!

    if(!(Eregi($pattern,$Url,$reg)))

    eregi_replace(‘http://’, $pattern, $Url);

    $WorkingKey = “MODULE_PAYMENT_CCAVENUE_WORKING_KEY”;

    $str =”$MerchantId|$OrderId|$Amount|$Url|$WorkingKey”;

    $adler = 1;

    $BASE = 65521 ;

    Error:
    Deprecated: Function eregi() is deprecated in
    public_html/store/includes/modules/payment/cavenue.php on line 61

    Deprecated: Function eregi_replace() is deprecated in public_html/store/includes/modules/payment/cavenue.php on line 62

  • rogerio24
  • Prosenjeet

    Any help with this will be appreciated:
    if (!preg_match(‘gigablast’, $_SERVER['HTTP_USER_AGENT']))

  • Prosenjeet

    OOps sorry……wrong code in my last comment
    Any help with this will be appreciated:
    if (eregi(‘gigablast’, $_SERVER['HTTP_USER_AGENT']))

  • PapiMigas

    thank you!

  • pito

    how I can change: (eregi(“(.*)/[^/]*”, $s, $out))
    P.