简体   繁体   English

从CSV文件获取随机电子邮件地址

[英]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. 我正在开发一个PHP应用程序,需要从CSV文件中提取5个随机电子邮件地址并将其发送给用户。

I already worked with CSV file many times but don't know how to fetch randomly in limit. 我已经使用CSV文件很多次了,但不知道如何在限制范围内随机获取。

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 如果CSV太大而无法保存在数据库中

You'll have to loop through all of the rows in the CSV once to count them. 您必须循环遍历CSV中的所有行才能对它们进行计数。

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). 您必须调用一个随机数生成器函数( randmt_rand ,others ...)并将其参数化以输出从0$count ,并调用5次(以获取5个数字)。

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. 您将不得不再次遍历CSV中的所有行,并且仅复制编号与随机生成的值匹配的行的必要信息。

Nota bene: don't use file_get_contents with str_getcsv . 注意:请勿将file_get_contentsstr_getcsv一起str_getcsv Instead use fopen with fgetcsv . 而是将fopenfgetcsv一起使用。 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 如果CSV太大,将保存在数据库中

Loop through the CSV rows and insert each record into the DB. 循环浏览CSV行,并将每个记录插入数据库。

Use a select query with LIMIT 5 and ORDER BY RAND() . 使用带有LIMIT 5ORDER BY RAND()的选择查询。

If the CSV is small enough to fit into memory 如果CSV足够小以适合内存

Loop through the CSV rows and create an array holding all of them. 遍历CSV行并创建一个包含所有行的数组。

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). 您必须调用一个随机数生成器函数( randmt_rand ,others ...),并将其参数化以输出从0到数组计数的数字,然后调用5次(以获取5个数字)。

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 如果csv文件不是太大,则可以将整个文件加载到数组中,以获得类似

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))]; 那么您可以通过e [rand(0,sizeof(e))]选择随机电子邮件;

and do this 5 times (with check for double items) 并执行5次(检查双项)

Read all emails from CSV then select random 5 email from email array. 从CSV中读取所有电子邮件,然后从电子邮件数组中随机选择5封电子邮件。 To select 5 random number use array_rand function. 要选择5个随机数,请使用array_rand函数。

$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. 这是一个小工具,可以实现您所期望的功能,并根据需要更改randomMail函数的声明。

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 从listmail.txt中读取列表邮件之后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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