Extract email addresses from a string – PHP

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

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 language: PHP (php)

..and this is how you use it:

$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 language: PHP (php)

Now let’s use the data:

// as string print(implode("\n", $emails)); // loop as array foreach($emails as $email) { echo $email .","; }
Code language: PHP (php)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments