Welcome to WebmasterWorld Guest from 54.221.9.209

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Sorting an email list with PHP?

     
1:59 pm on Jun 19, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 25, 2003
posts:2527
votes: 0


I have a ex-client email list of 10000+ email addresses. It looks something like this:

email1@company1.com
email2@company1.com
email3@company1.com
email1@company2.org
email2@company2.org
email1@company3.com
email2@company3.com
email3@company3.com
email4@company3.com

is there any way in PHP, Excel or anything - that will organise this list into separate files where only one occurrence of each company name is in each list? e.g.

LIST 1

email1@company1.com
email1@company2.org
email1@company3.com

LIST 2

email2@company1.com
email2@company2.org
email2@company3.com

LIST 3

email3@company1.com
email3@company3.com

LIST 4

email4@company3.com

I've been trying to do it manually, and my eyes are going fuzzy! It's taking forever.

Thanks
Mike
2:44 pm on June 19, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts: 15755
votes: 0


I might explode them into a db with fullemail, person, domain and then you could do selects with group by or distinct

you could then store other info and have a little crm going
5:57 pm on June 19, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 3, 2003
posts:1633
votes: 0


Hi Mike,

With the list saved as list.txt in the same folder, have a go with the following:


<?php
header("Content-Type: text/plain");
$lists = array();
$list = fopen("list.txt","r");
while(!feof($list))
{
$email = trim(fgets($list));
if ($email)
{
$parts = explode("@",$email);
// find the first list without an address for example.com ($parts[1])
$i = 1;
while(isset($lists[$i][$parts[1]])) $i++;
// ...and add the address
$lists[$i][$parts[1]] = $email;
}
}
//dump the lists
foreach($lists as $n => $emails)
{
print "List ".$n."\n\n";
print implode("\n",$emails)."\n\n";
}
?>