0

I have this code in my PHP form :

function IsEmail($email)
{
    $pattern = "^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,7}$";
    return (eregi($pattern,$email)) ? true : false;
};

And my local server gives me an error :

Function eregi() is deprecated

So I modified my code to :

function IsEmail($email)
{
    $pattern = "^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,7}$";
    return (preg_match($pattern,$email)) ? true : false;
};

and now I get this error :

Warning: preg_match() [function.preg-match]: No ending delimiter '^' found

Any idea ?

user2891155
  • 67
  • 3
  • 9
  • `return preg_match($pattern, $email);` is enough – Maxime Pacary Nov 07 '13 at 15:16
  • `eregi()` is a [POSIX](http://php.net/manual/en/book.regex.php) regex implementation, whereas `preg_*()` are [PCRE](http://php.net/manual/en/book.pcre.php) (Perl-Compatible) regex. As others have mentioned, it might be enough to add start and end delimiter characters, but it would be worth checking the manual for differences in syntax and function. – plasmid87 Nov 07 '13 at 15:18

2 Answers2

1

Add a delimiter.

$pattern = "/^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,7}$/";

In this case I used / as delimiter. It occurs once at the beginning and once at the end.

Note that you have to escape the delimiter character if it occurs in the regex itselfs and should not act as a delimiter but as normal matching char. So every / in your regex has to be escaped to \/ (except the delimiters of course). But you don't have any inside your regex.

TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
1

You should begin the regexp with / and also end it with /.

Like:

$pattern = "/^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,7}$/";
netdigger
  • 3,659
  • 3
  • 26
  • 49