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.

[raw]<pre>
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;
}</pre>
<p>[/raw]

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:

[raw]<pre>
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;
}</pre>
<p>[/raw]

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

$10.99 .CO Domains!

Incoming search terms:

  • Function eregi() is deprecated
  • Function ereg() is deprecated
  • Deprecated: Function eregi() is deprecated in
  • Deprecated: Function eregi() is deprecated
  • eregi
  • eregi deprecated
  • Function eregi() is deprecated in
  • Deprecated: Function ereg() is deprecated
  • Deprecated: Function ereg() is deprecated in
  • eregi is deprecated
  • 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

  • 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

  • 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??