Skip to content
Advertisement

Detect partial email pattern in PHP [closed]

I am writing an unsubscribe option of email newsletters.

I have to detect emails of following format:

<0 or more alphanumeric letter/digit only>, then one <@> character, then <1 or more alphanumeric letter/digit>, then a <.> character, then <at least 2 alphanumeric letter/digits>

I need “zero” or more alphanumeric character before @ character and not “one” or more because sometimes I want to unsubscribe whole domain names, so in that case the pattern to match is @example.com, and I also want to detect full email, it starts with an alphanumeric character.

How can I write the code to detect?

I take the email from url as $_GET['email']

For example url will be:

http://www.example.com/php/unsubscribe.php?email=@example.com
http://www.example.com/php/unsubscribe.php?email=@example.co
http://www.example.com/php/unsubscribe.php?email=abc@example.co

Advertisement

Answer

well the Regex then simply reads

/@.+.[^.]{2,}$/

EDIT: I used .* and [^.] even though the OP asked for “alphanumeric letter/digit” – however, valid E-Mail addresses include stuff like dashes, underscores etc… Completely matching ALL valid email adresses is incredibly complex! (see: http://www.regular-expressions.info/email.html)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement