2

I have a regexp to check email addresses that works in Javascript:

var re = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/; 
"[email protected]".search(re); // returns 0

But when I use preg_match() in PHP, using the same regexp and email it doesn't work!

$re = "/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/";
echo preg_match($re,"[email protected]");
// echoes 0 - not 1!!

EDIT

Okay, so I have to add more detail.

The full code is here:

    $re = '/^([0-9a-zA-Z]([-.\w\+\%\#\!\$\'\*\/\=\?\^\`\{\|\}\~]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/';

    if(is_array($recipients)){
        for($i=0;$i<count($recipients);$i++){
            echo preg_match($re,$recipients[$i]) . " - " . $recipients[$i] . "<br />";
        }
    }

When I just do the preg_match() (outside of the for loop), it will match the pattern properly. But if it's in this loop, then it won't validate!

6
  • 3
    No [email protected]? (Not to mention %,_ et al.) I'm a moderately sad panda. Commented Jan 13, 2012 at 15:42
  • 2
    Well that's odd. IDEOne seems to disagree. Commented Jan 13, 2012 at 15:42
  • Sorry piskvor, added support for a few more symbols according to en.wikipedia.org/wiki/Email_address#Syntax Commented Jan 13, 2012 at 15:54
  • 1
    Use a foreach, and consider filter_var($mail, FILTER_VALIDATE_EMAIL) instead. Your list of addresses most likely contains a linebreak or something. Give us a var_dump(). Commented Jan 13, 2012 at 16:00
  • Thank you mario! There was extra whitespace in the email addresses after the first one. I will use filter_var($mail, FILTER_VALIDATE_EMAIL) in place of preg_match in this case Commented Jan 13, 2012 at 16:04

3 Answers 3

4

You're forgetting that backslashes have special meaning in PHP when used within a double-quoted string.

Have you even echo $re; just to confirm the patter is what you expect it to be? I recommend switching to a single-quoted string for the pattern, or make sure you escape every \.

$re = "/^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$/"
$re = '/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/';

BTW, as others have mentioned, it does return 1.

I thought I would bring up escaping as it (even if not in this instance) can or will be an issue with patterns when in double quotes.

Sign up to request clarification or add additional context in comments.

5 Comments

That's only partially correct. For invalid escape sequences, PHP returns the input. echo "\w" actually outputs "\w".
@LinusKleen: Agreed, I've added a bit more explicit message to the bottom of my answer, but thanks for the feedback.
I echoed it and it's what I expected. I switched to single quotes, still no go.
@Martin: It has to be something on your end. I tried it (and posted the demo from ideone) that results in 1. Are there any other moving parts?
@Martin:What's recipients hold for information? Any spaces (maybe run it through a trim() before testing?)
0

It's commonly advised to use PHPs builtin regex for email validation, which has broader coverage of syntax variations:

filter_var($email, FILTER_VALIDATE_EMAIL)

See filter_var.
But btw, FILTER_VALIDATE_EMAIL is implemented using a regex too. See https://github.com/php/php-src/blob/master/ext/filter/logical_filters.c#L522 for that.

Not relevant for the whitespace issue you originally had, though.

1 Comment

I'm accepting this answer, as it is the correct way to validate an email. This, coupled with the trim() solution, solved my answer.
0

You need to use single quotes so escape characters are ignored.

$re = '/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/';
preg_match_all($re, '[email protected]', $arr, PREG_PATTERN_ORDER);
var_dump($arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.