getButterfly Logo getButterfly code wrangling since 2005

You might have a text file with a lot of emails, extracted from a mail client or a CSV file created with some script.

Nonetheless, you want to extract all the valid email addresses (user@domain.ext) from the file. Pass this function a file

(you can rename it to .php, or .txt, or .csv) and it will extract all addresses.

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



Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.

If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

2 thoughts on “Harvest Emails From Text File

Leave a reply

Love programming?

Learn about the most amazing things. Get smarter everyday!