Skip to content

Instantly share code, notes, and snippets.

@rquigley
Created August 15, 2011 19:39
Show Gist options
  • Save rquigley/1147580 to your computer and use it in GitHub Desktop.
Save rquigley/1147580 to your computer and use it in GitHub Desktop.
Extract any valid emails from a string
<?php
/**
* Extract any valid emails from a string
*
* @param string $str
* @return array matched email addresses
*/
function extractEmails($str)
{
$str = strtolower($str);
preg_match_all("/([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}/", $str, $vals);
$email_a = array_map(function($email) {
// Perform a more thorough validation
if (false === filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
} else {
return strtolower($email);
}
}, $vals[0]);
$email_a = array_unique($email_a);
return $email_a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment