Skip to content

Instantly share code, notes, and snippets.

@adaptive
Created August 25, 2008 18:28
Show Gist options
  • Save adaptive/7130 to your computer and use it in GitHub Desktop.
Save adaptive/7130 to your computer and use it in GitHub Desktop.
// place your text within the variable $text
// use this code to extract email addresses from a text
$text = ' email@domain.com ';
function parseTextForEmail($text) {
$email = array();
$invalid_email = array();
$text = ereg_replace("[^A-Za-z._0-9@ ]"," ",$text);
$token = trim(strtok($text, " "));
while($token !== "") {
if(strpos($token, "@") !== false) {
$token = ereg_replace("[^A-Za-z._0-9@]","", $token);
//checking to see if this is a valid email address
if(is_valid_email($email) !== true) {
$email[] = strtolower($token);
}
else {
$invalid_email[] = strtolower($token);
}
}
$token = trim(strtok(" "));
}
$email = array_unique($email);
$invalid_email = array_unique($invalid_email);
return array("valid_email"=>$email, "invalid_email" => $invalid_email);
}
function is_valid_email($email) {
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$",$email)) return true;
else return false;
}
var_dump(parseTextForEmail($text));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment