Sometimes you need to extract multiple email addresses from a string and the following function will make all your dreams come true.

[code lang=”php”]
function extract_emails_from($string){
preg_match_all(“/[\._a-zA-Z0-9-][email protected][\._a-zA-Z0-9-]+/i”, $string, $matches);
return $matches[0];
}
[/code]

..and this is how you use it:

[code lang=”php”]
$text = “this is some text and here is an email address [email protected], here’s another [email protected], etc..”;
$emails = extract_emails_from($text);
[/code]

Now let’s use the data:

[code lang=”php”]
// as string
print(implode(“\n”, $emails));

// loop as array
foreach($emails as $email) {
echo $email .”,”;
}
[/code]