How to read two lines at a time from a text file?

Asked

Viewed 253 times

-2

I have a text file where I need to read two lines at a time with each loop iteration. With this result I will mount a json. Simple example:

I have a text file:

JOAO PEREIRA
00000000000
PEDRO SILVA
11111111111
MARIA SOUSA
33333333333

I need to read two lines so that I can cut the strings and put it into an array to then mount the json. The way out of this is gonna stay that way:

"Registros": [
        {
            "nome": "Joao",
            "sobrenome:": "PEREIRA",
            "cpf:": "0000000000"
        },
        {
            "nome": "PEDRO",
            "sobrenome:": "SILVA",
            "cpf:": "1111111111"
        },
        {
            "nome": "MARIA",
            "sobrenome:": "SOUSA",
            "cpf:": "3333333333"
        }
  • 1

    If you went one line at a time, as you would?

2 answers

0


Here is a suggestion of PHP code for you to read two lines:

$txt - "arquivo.txt";
$hTxt = fopen($txt, 'r');

while (!feof($hTxt)) {
   $linha = fgets($hTxt);
   $nome1 = '';
   $nome2 = '';
   $cpf   = '';

   if (!feof($hTxt)) {
      $nome = explode(' ', $linha);
      $nome1 = $nome[0];
      $nome2 = $nome[1];

      $linha = fgets($hTxt);
      if (!feof($hTxt)){
         $cpf = $linha;
      }
   }

   // Aqui você joga o valor no JSON, para ler as próximas duas linhas
}
  • I didn’t understand this internal loop. Why you go through the entire file again to define $cpf? In that case $cpf would always be the last line and the external loop would terminate right after the first iteration, since you have already reached the end of the file.

  • Anderson, I fixed the code. I really didn’t need a second loop, just a reading of the next line. Thank you.

  • 2

    But checking to see if there is a next line will be quite useful if the file has an odd number of :D lines

  • Yes, Anderson, I agree. So much so that I expanded the code to clear the variables and go checking... Thanks for the note!

0

NOTE: The following code does not work very well with very large files and the line break may change depending on the operating system (where the file is), it is just an alternative using some php array functions, if you are using be aware of it.

 <?php
        //Extrai linhas do arquivo
        $contents = file_get_contents('https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10-million-password-list-top-100.txt');
        //Separa as linhas em um array
        $pieces = explode("\n", $contents);
        //Junta as linhas de 3 em 3
        $chunk = array_chunk($pieces, 3);
        //mapeia as linhas (operador de coalescência nula (??) disponível em PHP >=7)
        $map = array_map(function ($record) {
            return ["nome" => $record[0] ?? null, "cpf" => $record[1] ?? null, "sobrenome" => $record[2] ?? null];
        }, $chunk);
        //exibe json final
        echo json_encode($map);

Browser other questions tagged

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