-1

How can I replace the eregi on this code with pregmatch?

$subemail=$_POST['emailadd'];

$regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{1,})*\.([a-z]{2,}){1}$";

if(!eregi($regex, $subemail)){
//do this
}
SDFG
  • 3
  • 1

3 Answers3

1

Why you don't use filter_var to validate email?

var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));
Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
  • thanks. will return true but my complete algorithm needs more than just getting true or false. anyway, big thanks bro! – SDFG Jul 16 '13 at 07:57
0

Simply add delimiters(I use /) before and after your original regex and then add i flags at the end.

if(!eregi( "/". $regex ."/i", $subemail)){
  //do this
}
Hereblur
  • 2,084
  • 1
  • 19
  • 22
0

try the below 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.\";
} 
Jeet Singh
  • 402
  • 1
  • 5
  • 12