getButterfly Logo getButterfly

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

Usage:

<?php
extract_emails_from('myfile.txt');
?>


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

  1. This is an interesting post, I have been searching for a emails from texs file I will give this a try out over the next few week, thanks for sharing

Leave a Reply

Your email address will not be published. Required fields are marked *