Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    892
    Thanks
    301
    Thanked 2 Times in 2 Posts

    Question Regex or filter_var

    Hi All

    I was wondering which of the following is more reliable method to test a valid email address:

    1) Regex
    2) Using PHP's built-in function filter_var($email, FILTER_VALIDATE_EMAIL)


    Does the latter one follow wc3 standard?


    Thanks

  • #2
    New Coder
    Join Date
    Dec 2011
    Posts
    61
    Thanks
    4
    Thanked 10 Times in 10 Posts
    I've tried using RegEx with email, but it would block valid email addresses from time to time when I tested it out. So I just stick with filter_var($email, FILTER_VALIDATE_EMAIL) and that is my opinion what I this is best. However, you know what they say about opinions.
    True courage is about knowing not when to take a life, but when to spare one. PDO Login Tutorial

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I remember testing this way back when the filter var was first released, and found that FILTER_VAR did not appear to adhere to RFC standard (not w3, I don't know if / why they'd have an email specification since this has nothing to do with w3). It is however close enough.
    Writing a regex to adhere to RFC is also quite complex. Some dirty examples from wiki to give you an idea:
    Code:
    postbox@com
    !#$%&'*+-/=?^_`{}|~@example.org
    " "@example.org
    Are all valid examples.

    My suggestion? Use the filter_var. The only time you may have problems is likely the same problems you'd have with regex.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •