Skip to content

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[]; }
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)

See also  phpMyAdmin - Invalid field count in csv input on line 1
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x