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!
[email protected]? (Not to mention%,_et al.) I'm a moderately sad panda.foreach, and considerfilter_var($mail, FILTER_VALIDATE_EMAIL)instead. Your list of addresses most likely contains a linebreak or something. Give us a var_dump().