$email = "the email";
$firstName = "the first name";
$lastName = "the last name";
$new_line = "$email|$firstName|$lastName\n"; // | could be other character
$file = fopen("subscribers.txt", "a");
fputs($file, $new_line);
fclose($file);
读取和解析数据
$subscribers = array();
$handle = @fopen("subscribers.txt", "r");
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle, 4096);
//parsing the line
$ar = explode('|', $line);
//$ar[0] holds the email
if(key_exists(0, $ar)){
$email = $ar[0];
}else{
$email= '';
}
//$ar[1] holds the first name
if(key_exists(1, $ar)){
$firstName = $ar[1];
}else{
$firstName = '';
}
//$ar[2] holds the last name
if(key_exists(2, $ar)){
$lastName = $ar[2];
}else{
$lastName = '';
}
$temp = array(
'email' => $email,
'firstName' => $firstName,
'lastName' => $lastName
);
$subscribers[] = $temp;
//
}
fclose($handle);
}
The database way is, indeed, the best one. If you rather use the text file approach i would suggest something like this:
Inserting Data into the file
$email = "the email";
$firstName = "the first name";
$lastName = "the last name";
$new_line = "$email|$firstName|$lastName\n"; // | could be other character
$file = fopen("subscribers.txt", "a");
fputs($file, $new_line);
fclose($file);
Reading and Parsing Data
$subscribers = array();
$handle = @fopen("subscribers.txt", "r");
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle, 4096);
//parsing the line
$ar = explode('|', $line);
//$ar[0] holds the email
if(key_exists(0, $ar)){
$email = $ar[0];
}else{
$email= '';
}
//$ar[1] holds the first name
if(key_exists(1, $ar)){
$firstName = $ar[1];
}else{
$firstName = '';
}
//$ar[2] holds the last name
if(key_exists(2, $ar)){
$lastName = $ar[2];
}else{
$lastName = '';
}
$temp = array(
'email' => $email,
'firstName' => $firstName,
'lastName' => $lastName
);
$subscribers[] = $temp;
//
}
fclose($handle);
}
For looping through the subscribers and using you function to send email
发布评论
评论(1)
数据库方式确实是最好的一种。如果您更愿意使用文本文件方法,我会建议这样的操作:
将数据插入文件
读取和解析数据
For循环订阅者并使用您的函数发送电子邮件
The database way is, indeed, the best one. If you rather use the text file approach i would suggest something like this:
Inserting Data into the file
Reading and Parsing Data
For looping through the subscribers and using you function to send email