Which Method For Verification ? REGEX or filter_var?

480 Views Asked by At

What do you think, is there any difference (for performance, security, reliability ) between this methods :

METHOD 1

eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)

METHOD 2

filter_var($email, FILTER_SANITIZE_EMAIL) 
filter_var($email, FILTER_VALIDATE_EMAIL)
2

There are 2 best solutions below

1
On BEST ANSWER

Method 2 is clearly easier to understand and more maintainable. The regular expression just looks like line noise.

1
On

If method 2 is a good email validation then thats better.

Method 1 could be cleaned up if case insensitive to this (expanded)

Correction: did not read the regex close enough.

Stick with method 2 ..