views:

37

answers:

2

I am recieving the error:

Deprecated: Function eregi() is deprecated in C:\wamp\www\registration\class.register.php on line 75

with my code::

if(empty($this->email) || !eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$',$this->email))

What alternative should I use and how can I implement it????

+1  A: 

Yes ereg family functions have been deprecated, you need to use preg family functions instead. In your case, you should use preg_match instead.

That piece of code is equivalent to:

if(empty($this->email) || 
    !preg_match('~^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$~i',
    $this->email))

It can also be compacted to:

if(empty($this->email) || !preg_match('~^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,4}$~i',
    $this->email))
Sarfraz
+3  A: 

As @Sarfraz said ereg_* functions are deprecated and you should use preg_* instead. However in this case you shouldn't use regular expressions at all. There is function called filter_var() that allows you to validate some popular data formats (emails, URLs etc.)

if (empty($this->email) || false == filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
    // Empty or not valid email
}
Crozin
Notabede email is an URL. ;)
Crozin