Created
August 15, 2011 19:39
-
-
Save rquigley/1147580 to your computer and use it in GitHub Desktop.
Extract any valid emails from a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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