ionicle Posted November 20, 2013 Share Posted November 20, 2013 (edited) 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 November 20, 2013 by ionicle Quote Link to comment Share on other sites More sharing options...
MDCode Posted November 20, 2013 Share Posted November 20, 2013 You have to give more information. How is the list provided? Is it a giant string separated by commas? Is it an array? Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted November 20, 2013 Share Posted November 20, 2013 (edited) You could try something like the following: Put the email addresses in an array Use array_rand() to get a random email Save the random email into a new list. Grab the domain with explode() and store it in another array used for testing Get another random email address with array_rand(). See if the domain of the newly chosen email is already in the array of picked domains with in_array() 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 November 20, 2013 by cyberRobot Quote Link to comment Share on other sites More sharing options...
denno020 Posted November 20, 2013 Share Posted November 20, 2013 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 Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted November 20, 2013 Solution Share Posted November 20, 2013 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>'; ?> Quote Link to comment Share on other sites More sharing options...
ionicle Posted November 20, 2013 Author Share Posted November 20, 2013 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.comkochina@yahoo.combrym@gmail.comshushlek@gmail.comkurnica@gmail.comshushlica@yahoo.com However, when I execute the php, I get the following output: kochina@yahoo.comkurnica@gmail.comshushlica@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? Quote Link to comment Share on other sites More sharing options...
MDCode Posted November 20, 2013 Share Posted November 20, 2013 (edited) You could just use file(); Edit: $array = file($filename); print_r($array); Edited November 20, 2013 by SocialCloud Quote Link to comment Share on other sites More sharing options...
denno020 Posted November 20, 2013 Share Posted November 20, 2013 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. Oops! I didn't even think.. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted November 20, 2013 Share Posted November 20, 2013 Oops! I didn't even think.. No worries Quote Link to comment Share on other sites More sharing options...
ionicle Posted November 22, 2013 Author Share Posted November 22, 2013 Thank you everyone for the help! Issue was sorted out and everything's working like a charm now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.