This is a function that you can embed in your PHP applications that will help you extract email addresses from a given piece of text.
Tested on a string of (actually 4 - 5 paragraphs) text and this has performed very well.
Please feel free to use this code in your applications and let me know if you face any issues (http://www.navioo.com)
<?
$text = ' Montreal based PR firm seeking staff to work events, promotions and marketing campaigns. All candidates must be bilingual, work well with others, outgoing, charismatic, and energetic.(Experience is an asset).
<br><br>
For consideration kindly forward a copy of your CV and a recent photo of yourself to mybest@bizcaf.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;
}
$retEmail=parseTextForEmail($text);
var_dump($retEmail);
if(count($retEmail["valid_email"])>0){
$email=$retEmail["valid_email"][0];
}
echo $email;
?>
|