17

I'm trying to submit values to the database but i get an error message

Deprecated: Function eregi() is deprecated in C:\wamp\www\OB\admin_add_acc.php on line 20 and 27

Here is the code:

<?php       

include 'db_connect.php'; 

if(isset($_POST['Submit']))           
{            
$acc_type=ucwords($_POST['acc_type']);
$minbalance=ucwords($_POST['minbalance']);                       
if (!eregi ("^[a-zA-Z ]+$", stripslashes(trim($acc_type))))//line 20 
{                 
echo "Enter Valid Data for Account Type!";                
exit(0);                 
}           
else 
{                  
if (!eregi ("^[0-9 ]+$", stripslashes(trim($minbalance))))//line 27
{                       
Bora
  • 10,529
  • 5
  • 43
  • 73
Jush
  • 193
  • 1
  • 3
  • 9
  • 2
    Use `preg_match` instead of `eregi` – Bora Aug 29 '13 at 11:35
  • Well.. that's because `ereg*` functions are deprecated. That exactly what [manual says](http://php.net/manual/en/migration53.deprecated.php) – Alma Do Aug 29 '13 at 11:36
  • so many duplicates to this. *sooooooo* many. – Spudley Aug 29 '13 at 11:55
  • Just add @ in front of the function. e.g. @ereg() – Jide Apr 14 '14 at 00:07
  • 1
    For others like me that find this post here, and just need to check for 1 word, instead of `if(eregi('VERIFIED', $info)) {` use `if(preg_match('/VERIFIED/i', $info)) {`. The [i flag](http://php.net/manual/en/function.preg-match.php#refsect1-function.preg-match-examples) indicates a case-insensitive search. Guess this helps too. – Avatar Jan 02 '15 at 13:22

3 Answers3

24

eregi() is deprecated as of PHP 5.3, use preg_match() instead.

Note that preg_match() is only case insensitive when you pass the i modifier in your regular expression.

include 'db_connect.php'; 
if(isset($_POST['Submit']))           
{            
    $acc_type=ucwords($_POST['acc_type']);
    $minbalance=ucwords($_POST['minbalance']);
    
    // Removed A-Z here, since the regular expression is case-insensitive                
    if (!preg_match("/^[a-z ]+$/i", stripslashes(trim($acc_type))))//line 20 
    {                 
        echo "Enter Valid Data for Account Type!";                
        exit(0);                 
    }           
    else 
    {                  
        // \d and 0-9 do the same thing
        if (!preg_match("/^[\d ]+$/", stripslashes(trim($minbalance))))//line 27
        {
        }
    }
} 
mwfearnley
  • 3,303
  • 2
  • 34
  • 35
MisterBla
  • 2,355
  • 1
  • 18
  • 29
2

From Wikipedia:

Deprecation is a status applied to a computer software feature, characteristic, or practice indicating it should be avoided, typically because of it being superseded.

Take a look at the PHP manual for eregi. As you can see, it has the following warning:

This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

Further down the page there is some advice on what to use instead:

eregi() is deprecated as of PHP 5.3.0. preg_match() with the i (PCRE_CASELESS) modifier is the suggested alternative.

So you can use the preg_match function instead.

OdinX
  • 4,135
  • 1
  • 24
  • 33
0

You can find the answer here in the manual.Since its a Deprecated function in the php version you are using you will get that warning.Instead of ergi you can use preg_match.See the manual for preg match

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91