简体   繁体   中英

Get random email address from CSV file

I am developing a PHP application where I need to fetch 5 random email addresses from a CSV file and send to user.

I already worked with CSV file many times but don't know how to fetch randomly in limit.

NOTE: CSV file have more than 200k emails.

Any one have a idea or suggestion then please send me.

If CSV is too big and won't be saved in a DB

You'll have to loop through all of the rows in the CSV once to count them.

You'll have to call a random-number generator function ( rand , mt_rand , others...) and parametrize it to output numbers from 0 to $count , and call it 5 times (to get 5 numbers).

You'll have to loop through all of the rows in the CSV again and only copy the necessary information for the rows whose number matches the randomly generated values.

Nota bene: don't use file_get_contents with str_getcsv . Instead use fopen with fgetcsv . The first approach loads the entire file to memory which we don't want to do. The second approach only read the file line-by-line.

If the CSV is too big and will be saved in a DB

Loop through the CSV rows and insert each record into the DB.

Use a select query with LIMIT 5 and ORDER BY RAND() .

If the CSV is small enough to fit into memory

Loop through the CSV rows and create an array holding all of them.

You'll have to call a random-number generator function ( rand , mt_rand , others...) and parametrize it to output numbers from 0 to array count, and call it 5 times (to get 5 numbers).

Then retrieve the rows from the big array by their index number -- using the randomly generated numbers as indexes.

If csv file is not too big you can load whole file to array to get something like

e[0] = 'someone1@somewhere.com';
e[1] = 'someone2@somewhere.com';
e[2] = 'someone3@somewhere.com';

then you can pick random email by e[rand(0, sizeof(e))];

and do this 5 times (with check for double items)

Read all emails from CSV then select random 5 email from email array. To select 5 random number use array_rand function.

$email = array('test@test.com','test2@test.com','test3@test.com','test4@test.com','test5@test.com');
$output = array_rand($email, 5);

print_r($email); // will return random 5 email.

for large number try to use something like

$max = count($email);
$email_rand = array();
for ($i =0; $i<5; $i++) 
{
    $a = mt_rand(0, $max);
    $email_rand[] = $email[$a];
}
print_r($email_rand);
<?php
$handle = fopen('test.csv', 'r');
$csv = fgetcsv($handle);

function randomMail($key)
{
    global $csv;
    $randomMail = $csv[$key];
    return $randomMail;
}

$randomKey = array_rand($csv, 5);
print_r(array_map('randomMail', $randomKey));

This is small utility to achieve the thing you expect and change the declaration of randomMail function as you desired.

for($i=0;$i<5;$i++)
{
$cmd = "awk NR==$(($"."{RANDOM} % `wc -l < ~/Downloads/email.csv` + 1)) ~/Downloads/email.csv >> listemail.txt";
$rs = exec($cmd);
}

after you read list mail from listmail.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM