Strip e-mail addresses from a string in PHP

821 views Asked by At

I operate an archive of e-mail for a law firm that receives mail from Postfix and uses a PHP script to insert messages into a database. This works mostly fine but sometimes the regular expression I use to parse e-mail addresses from the From, To, and Cc headers does not capture e-mail addresses with 100% accuracy. I have tried the other solutions posited here on stackoverflow (using filter_var(), using imap_rfc822_parse_adrlist, using the regex in question 1028553) with actually less success than what I have.

I am looking to minimize system calls (I use way too many pregs right now) and increase accuracy. The current function takes an input of header text (the From, To, or Cc fields) and returns "clean" e-mail addresses stripped of brackets, quotes, comments, etc.

Any help anyone can provide would be appreciated, as I am stumped!

Wendy

My function:

function return_proper ($email_string) {
if (is_array($email_string)) {
    $x = "";
    foreach ($email_string as $val) {
        $x .= "$val,";
    }
    $email_string = substr($x, 0, -1);
}

$email_string = strtolower(preg_replace('/.*?([A-Za-z0-9\_\+\.\'-]+@[A-Za-z0-9\.-]+).*?/', '$1,', $email_string));
$email_string = preg_replace('/\>/', "", $email_string);
$email_string = preg_replace('/,$/', "", $email_string);
$email_string = preg_replace('/^\'/', "", $email_string);
return $email_string;
}
1

There are 1 answers

0
Rannnn On
function return_proper($email_string) {
    if (is_array($email_string)) {
        // Deal with array
        foreach ($email_string as $email_string_line) {
            $results[] = return_proper($email_string_line);
        }

        $result = implode(',', $results);
    } else {
        preg_match_all('/[A-Za-z0-9\_\+\.\'-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]+/', $email_string, $matches);

        $result = implode(',', $matches[0]);
    }

    return strtolower($result);
}