tags:

views:

80

answers:

2

I have approximately 96K text emails that I want to extract the sender's address for. I believe that I can use domdoc for this but need someone to start me off. Can someone please advise whether there is a better way of doing this?

Thanks, Jim

A: 

Using a regular expression in some form would be the best way to do it. If you can save your text emails to files, you can use something like Textpad to search for email addresses based on the regular expression.

You should be able to find regular expressions for email addresses online.

Uprock7
Hey Uprock, the problem is that there are 96 thousand emails so I don't think I can load that many. I can in fact save each email to its own file.
Jim
Wrap the regular-expression part in a simple shell that iterates through emails, then. You don't need to keep them all in memory at once.
JSBangs
You could just use the "grep" command, or something along those lines. It's designed to search large files for regex matches without loading the entire file into memory.
Frank Farmer
Textpad has a "Find In Files" option where you point to a directory and it will find matches to your search in all the files in the directory. I have used it to search through thousands of files and not have had a problem with it before.
Uprock7
+1  A: 

See no reason to do this in PHP... Provided the files are in some form of flat text, copy the file(s) to (for example) the emails/ directory, then

cat * | grep "From: " | egrep -oi ‘\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}’ | sort | uniq > mail.list

Of course if you have to do this in PHP then

  1. Copy the files/mails to a directory
  2. Get a list of the files with readdir()
  3. Read the file(s)
  4. Split the header from to a separate string
  5. Do a preg_match() on this string to find an email address and put it to $email_arr
  6. When finished, do array_unique() on the $email_arr.
matiasf
useless use of cat.
Yepp, but I like to start my pipes with cat.
matiasf