0

I am following a book to learn PHP and I had a question!

This is part of the code for the email validation:

$pattern = '/\b[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}\b/';
if(!preg_match($pattern, $email))
{ $email = NULL; echo 'Email address is incorrect format'; }

Can someone explain to me what '$pattern' is doing? I am not sure, but from what I previously know about coding with application that connect to websites, I think it may be something called "Regex"?

If anyone can explain that line to me, I appreciate it. Also if it is "Regex," can you provide a link to somewhere that just gives a brief explanation of what it is and how it works?

Karl
  • 61
  • 1
  • 9
  • And for that specific case: [How to validate an email address in PHP](http://stackoverflow.com/q/12026842) – mario Feb 08 '15 at 21:36

1 Answers1

1

A regex is a regular expression: it's a pattern that describes a set of strings, typically a subset of the set of all possible strings. All special characters a regex can use are explained in the question that your question has been marked a duplicate of.

But specifically for your case; there's a nice tool that can explain regexes here:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  [\w.-]+                  any character of: word characters (a-z, A-
                           Z, 0-9, _), '.', '-' (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  @                        '@'
--------------------------------------------------------------------------------
  [\w.-]+                  any character of: word characters (a-z, A-
                           Z, 0-9, _), '.', '-' (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  [A-Za-z]{2,6}            any character of: 'A' to 'Z', 'a' to 'z'
                           (between 2 and 6 times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char

Validating email addresses, the right way

But if you're using PHP >= 5.2.0 (which you probably are), you don't need to use a regex for that. It's much clearer code to use the built-in filter_var():

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // email valid
} else {
    // email invalid
}

You don't have to worry about boundary cases or anything.