1

please when using the eregi() function to validate an email address i got this error:

Deprecated: Function eregi() is deprecated in C:\wamp\www\ssiphone\classes\TraitementFormulaireContact.php on line 13

my code which may make problem is :

 public function verifierMail($mail)
 {
    if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $mail)) {
      return "valid mail";
    }
    else {
      return "invalid mail";
    }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Malloc
  • 15,434
  • 34
  • 105
  • 192
  • 1
    Your regular expression is invalid. Use [`filter_Var()` function](http://pl.php.net/manual/en/function.filter-var.php) - it's a way easier that writing your own expression. – Crozin Mar 23 '11 at 17:27

3 Answers3

3

the eregi function is deprecated, which means that in future versions of PHP it will be removed.

You can replace it with the function preg_match which does pretty much the same thing.

Sample code (untested):

public function verifierMail($mail)
{
if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $mail)) {
  return "valid mail";
}
else {
  echo "invalid mail";
}

The /i makes it case insensitive

Nico Burns
  • 16,639
  • 10
  • 40
  • 54
2

use the function preg_match() instead

you can find the php manual page here: http://us3.php.net/manual/en/function.preg-match.php

Chalise
  • 3,656
  • 1
  • 24
  • 37
1

Aside from substituting ereg_* with preg_*, you should consider the builtin filter_var() function:

filter_var($mail, FILTER_VALIDATE_EMAIL)

you'll still get false negatives (there are a lot of valid emails you'd never imagine), but it's still better than a poor regexp.

Matteo Riva
  • 24,728
  • 12
  • 72
  • 104