-2

I have a function (that works) and checks if an email address is valid. I understand how most of it is working except for the part that actually tests when there is a decent looking email.

How does this work:

 if ( preg_match("/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i", $email) ) {
        $valid_address = true;
      } else {
        $valid_address = false;
      }
    } 

I don't really understand preg_match, so this looks like pretty much random characters.

Sackling
  • 1,724
  • 5
  • 35
  • 70
  • 1
    If you think that regex is bad, try the [RFC-compliant one](http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html) on for size. Heh. – ceejayoz Jul 12 '13 at 14:17

2 Answers2

4

Don't use preg_match to validate e-mails. PHP has a built in function for this.

filter_var('bob@example.com', FILTER_VALIDATE_EMAIL)

That said, preg_match's operators are well documented.

ceejayoz
  • 171,474
  • 40
  • 284
  • 355
  • Cool. Did not know that existed. This is part of a larger project (oscommerce) but I can pretty easily change it over. – Sackling Jul 12 '13 at 14:14
1

First, preg_match has documentation here: http://php.net/manual/en/function.preg-match.php

but what it sounds like you really want to know is regular expressions(that string of crazy characters): Learning Regular Expressions

The second answer has a decent overview.

Community
  • 1
  • 1
Snagulus
  • 132
  • 5