Saturday, December 27

Check multiple e-mail Id from a file PHP

    $file_string = file_get_contents("example.txt"); // Load text file contents
    $matches_email_id=array();

// don't need to preassign $matches, it's created dynamically


// this regex handles more email address formats like a+b@google.com.sg, and the i makes it case insensitive

    $reg_pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';
/*******************OR******************/
    $reg_pattern = '/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/';

// preg_match_all returns an associative array

    preg_match_all($reg_pattern, $file_string, $matches_email_id);

// the data you want is in $matches[0], dump it with var_export() to see it

    var_export($matches_email_id[0]);


OUTPUT:


array (

  0 => 'test1+2@gmail.com',
  1 => 'test-2@yahoo.co.jp',
  2 => 'test@test.com',
  3 => 'test@test.co.uk',
  4 => 'test@google.com.sg',
)

No comments:

Post a Comment