How to read a file with an email list?

Asked

Viewed 600 times

8

I have a text file with many emails and I want to save everything in a database, as I do to get each email from the file in PHP?

The emails are separated by comma.

  • Exist only emails in this list?

  • Yeah, it’s just emails

2 answers

7


You can use the function file() to read the e-mails and put them in a array.

$texto = file('emails.txt');

Now just use the explode() to capture the emails and separate them by the comma.

$emails = explode(",", $texto);
  • Only additional reading: http://php.net/manual/en/function.fgetcsv.php

2

Starting from the pre-presumed that the bank has the emails table, and that the file is teste.txt:

// Pega o conteudo do arquivo teste.txt
$arquivo_texto = file_get_contents('teste.txt');

// Cria um array com os emails do arquivo teste.txt
$emails = explode(',', $arquivo_texto);

// Abre uma conexão com o banco de dados mysql
$con = new PDO("mysql:host=localhost;dbname=nome_do_banco", "root", "");

// Loop para inserção no banco de dados
foreach ($emails as $value) {
 $rs = $con->query("INSERT INTO emails VALUES ?");
 $rs->bindParam(1, $value);
 $rs->execute();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.