How can PHP be used to read email addresses and names from a TXT file for newsletter distribution?
To read email addresses and names from a TXT file for newsletter distribution using PHP, you can create a script that reads the file line by line, extracts the email addresses and names, and stores them in an array or database for further processing.
<?php
// Open the TXT file for reading
$file = fopen('emails.txt', 'r');
// Initialize an empty array to store email addresses and names
$contacts = [];
// Read the file line by line
while (!feof($file)) {
$line = fgets($file);
$data = explode(',', $line);
// Extract email address and name
$email = trim($data[0]);
$name = trim($data[1]);
// Store email address and name in the contacts array
$contacts[] = ['email' => $email, 'name' => $name];
}
// Close the file
fclose($file);
// Output the contacts array for further processing
print_r($contacts);
?>
Related Questions
- In what ways can SQL queries be optimized in PHP to avoid unnecessary repetition and improve performance when retrieving data for different categories and months?
- What are the best practices for handling PHP warnings and errors in a server environment?
- Are there any potential pitfalls or limitations when using regular expressions to parse text files in PHP, and how can they be mitigated?