-1

Can someone explain to me? This is my code :

 $regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+'
            . '(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
    if (preg_match($regex, $email)) {
        echo $email . " is a valid email. We can accept it.";
    } else { 
        echo $email . " is an invalid email. Please try again.";
    }

If I use for an example this regular expression:

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

or another different regular expression I get:

preg_match() [function.preg-match]: No ending delimiter '^' found

And if I use filter_var('some_regular_expression', FILTER_EMAIL_VALIDATOR) an email like asd!qwe@gmail.com is a valid one ???

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • There appears to be a unicode character in your expression throwing off the parser, but Barmar is right, you need to use a delimeter for your expression. Common ones are `/../` or `~..~` –  Nov 21 '13 at 19:56

1 Answers1

1

When you use the preg_XXX functions, the first character of the regular expression argument is a delimiter character -- it's / in your first example. It must appear at the end of the regular expression, it's used to separate the regular expression from modifier option characters, for instance in:

/[a-z]/i

the second / separates the regular expression from the i (case-insensitive) modifier.

So you simply need to change your regular expression to:

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

I'm not sure what that character is in there for, though. It doesn't seem like it's appropriate for validating emails.

And yes, asd!qwe@gmail.com is a valid email. Back in the day of UUCP mail transfer, addresses of the form host1!host2!host3!username@uu.net were common -- the ! characters delimited hostnames in the UUCP path. See Using a regular expression to validate an email address for recommendations on email validation.

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • nah , i've checked mails like !asd@gmail.com , asd!ko@gmail.com are not valid emails, but thanks for your answer you helped me with the regular expression. – user2894433 Nov 21 '13 at 19:56
  • 1
    Maybe Gmail doesn't allow those types of names, but they're allowed by the SMTP standard. – Barmar Nov 21 '13 at 19:57