27 December 2013

How to Extract Email Address From String

Here is a function to Extract all the Email Addresses from String...
This function will return an array of found email ids...

function extractEmailsFromString($string)
{
 $emails = array();
 $string = str_replace("rn",' ',$string);
 $string = str_replace("n",' ',$string);
 foreach(preg_split('/s/', $string) as $token)
 {
  $email = filter_var(filter_var($token, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
  if ($email !== false) {
   $emails[] = $email;
  }
 }
 return $emails;
}

No comments:

Post a Comment