0

Good day, I need to find with a regular expression all matching elements in an array using PHP, but I have a bug.

<?php $a[0]='[email protected]'; $a[1]='[email protected]'; $a[2]='[email protected]'; $a[3]='[email protected]'; $pattern = '[a-z_0-9]+@[a-z]*.[a-z]{2,}'; for ($i=0;$i<=3;++$i) { if(preg_match($pattern,$a[i])) { echo $a[i]; } } 

Warning: preg_match() [function.preg-match]: Unknown modifier '+' in {path to file} on line 9

Thanks.

2
  • try to put in double quotes. Commented Jan 10, 2013 at 14:17
  • it doesn't matter, which quotes used in regex., but I've tried every variant Commented Jan 10, 2013 at 14:29

2 Answers 2

2

No need for regex here -- PHP has a built-in email validation function:

filter_var( $email, FILTER_VALIDATE_EMAIL ); 

See manual page here: http://php.net/manual/en/function.filter-var.php

There's even a filter_var_array() which will save you from even having to have a loop.

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

1 Comment

I just learn and not specifically apply, but thanks for additional information
0

You can try this-

$a[0]='[email protected]'; $a[1]='[email protected]'; $a[2]='[email protected]'; $a[3]='[email protected]'; for ($i=0;$i<=3;++$i) { if (!preg_match("/^[-_\.a-z0-9]+@([-_a-z0-9]+\.?)*[a-z]{2,6}$/i", $a[$i])) { return false; } return true; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.