Jump to content

Pick a random email address out of a maillist


ionicle
Go to solution Solved by cyberRobot,

Recommended Posts

Hey everybody.

 

Trying to figure out how to complete a certain task here, and I am pretty sure it can be done via php. Not sure how to start implementing it though, so any suggestions would be hugely appreciated.

 

What I wanna accomplish is, as follows:

 

Out of a list of email addresses, I would like php to pick ONE RANDOM email address out of every domain name, listed in there, for instance:

 

dave@yahoo.com

john@yahoo.com

michael@hotmail.com

jason@hotmail.com

etc.

 

Just one random email addy with a certain domain name ( namely, the part after the @ sign ). The ending result out of the example, listed above, ought to be:

 

dave@yahoo.com

jason@hotmail.com

 

That's it, pretty much. Nothing more, nothing less.

 

Open to suggestions on how to start this thing!

Edited by ionicle
Link to comment
Share on other sites

You could try something like the following:

  1. Put the email addresses in an array
  2. Use array_rand() to get a random email
  3. Save the random email into a new list.
  4. Grab the domain with explode() and store it in another array used for testing
  5. Get another random email address with array_rand().
  6. See if the domain of the newly chosen email is already in the array of picked domains with in_array()
  7. If the domain has already been chosen from, get the next email. If not, store email and domain and continue processing the emails until done.

 

Of course, I may have missed a step or two, but this should hopefully get you started.

Edited by cyberRobot
Link to comment
Share on other sites

The way I would do it is as follows:

Loop through all email addresses, matching the domain name for each, and using that as a key in an array... actually it'll just be easier if I show you code

$emailAddresses = array() //This is the email addresses you have to start with, it will hopefully be an array. If it's a string, use explode() to get it into an array
$sortedEmails = array(); //Will store the sorted email addresses
foreach ($emailAddresses as $emailAddy) {
    preg_match("/.*@(.*)$/", $emailAddy, $matches);
    $domain = $matches[1];
    if (!isset($sortedEmails[$domain])) { //If the domain doesn't already have it's own array, create it
        $sortedEmails[$domain] = array();
    }
    $sortedEmails[$domain][] = $emailAddy; //Add the email address to the end of the array for the domain
}
 
//Pick random email
foreach ($sortedEmails as $domain) {
    echo $domain[rand(0, count($domain)-1]; //Pick a random number between 0 and the size of the array minus 1, which will effectively be a random index
}

Hopefully that helps.

 

Denno

Link to comment
Share on other sites

  • Solution

This is probably doing homework, but since someone has already provided a solution [and I was intregued]...I thought I would work up a solution.

<?php
//INITIALIZE ARRAYS
$emails        = array('1@yahoo.com', '2@yahoo.com', '3@yahoo.com', 'G1@gmail.com', 'G2@gmail.com', 'G3@gmail.com', 'another@somewebsite.com', 'yetmore@zd.com', 'yetmoremore@zd.com');
$newEmails     = array();
$usedDomains   = array();
$uniqueDomains = array();
 
//WHILE THERE ARE EMAILS AVAILABLE, PROCESS THE NEXT RANDOM ENTRY
while(!empty($emails)) {
     //GET RANDOM EMAIL
     $randomEntryID = array_rand($emails);
     $randomEntry   = $emails[$randomEntryID];
     $currentDomain = substr(strrchr($randomEntry, '@'), 1);
 
     //UPDATE ARRAYS
     $newEmails[]   = $randomEntry;
     $usedDomains[] = $currentDomain;
     unset($emails[$randomEntryID]);
}
 
//REMOVE DUPLICATE DOMAINS (ALSO GIVES THE INDEX WHERE THE DOMAIN WAS FIRST USED)
$uniqueDomains = array_unique($usedDomains);
$uniqueIDs     = array_keys($uniqueDomains);
 
//DISPLAY EMAILS OF INTEREST
foreach($uniqueIDs as $currID) {
     print '<div>' . $newEmails[$currID] . '</div>'; 
}
 
//SHOW WHAT THE ARRAYS CONTAIN
print '<pre>' . print_r($newEmails, true) . '</pre>';
print '<pre>' . print_r($usedDomains, true) . '</pre>';
print '<pre>' . print_r($uniqueDomains, true) . '</pre>';
print '<pre>' . print_r($uniqueIDs, true) . '</pre>';
?>
Link to comment
Share on other sites

Last suggestion works perfectly! The only thing I need now is to load a text file into the array, and avoid having to input email addresses manually.

 

 

 
 
 
<?php

// Open the file
$filename = "test.txt";
$fp = @fopen($filename, 'r');

// Add each line to an array
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}

//INITIALIZE ARRAYS
$emails = $array;
$newEmails = array();
$usedDomains = array();
$uniqueDomains = array();

//WHILE THERE ARE EMAILS AVAILABLE, PROCESS THE NEXT RANDOM ENTRY
while(!empty($emails)) {
//GET RANDOM EMAIL
$randomEntryID = array_rand($emails);
$randomEntry = $emails[$randomEntryID];
$currentDomain = substr(strrchr($randomEntry, '@'), 1);

//UPDATE ARRAYS
$newEmails[] = $randomEntry;
$usedDomains[] = $currentDomain;
unset($emails[$randomEntryID]);
}

//REMOVE DUPLICATE DOMAINS (ALSO GIVES THE INDEX WHERE THE DOMAIN WAS FIRST USED)
$uniqueDomains = array_unique($usedDomains);
$uniqueIDs = array_keys($uniqueDomains);

//DISPLAY EMAILS OF INTEREST
foreach($uniqueIDs as $currID) {
print '<div>' . $newEmails[$currID] . '</div>';
}


?>
 

 

 

The .txt file contains the following test emails:

 

laina@yahoo.com
kochina@yahoo.com
brym@gmail.com
shushlek@gmail.com
kurnica@gmail.com
shushlica@yahoo.com

 

However, when I execute the php, I get the following output:

 

 

kochina@yahoo.com
kurnica@gmail.com
shushlica@yahoo.com

 

There's only 2 types of domains in those email addresses, and I always get 1 of one domain and 2 of the other, instead of one of each.

 

What am I missing here?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.