Tutorial Extract Email Addresses

by in , 0

Just pass the string (e.g. the body part of an email) to the function, and it returns an array of Email Addresses contained in the String.

function extract_emails_from($string) {
         preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
         return $matches[0];
}

If you catch the return value of function in $emails, you can parse it using foreach:

foreach($emails as $email) {
    echo trim($email).'<br/>';
}

Leave a Reply