Problem reading txt file with PHP - fgets does not take the next line

0
Is everything good? I have a problem with reading a specific txt file. As you can see below my code uses fgets to read line by line.

<?php

extrairDadosNotificacao("arquivo1.txt");
extrairDadosNotificacao("arquivo2.txt");

function extrairDadosNotificacao($NomeArquivo){
      $arquivo = fopen($NomeArquivo, 'r');
      while(!feof($arquivo)) {
          $linha = fgets($arquivo);
          echo $linha . "<br>";
      }
}

A file returns exactly what I expect, which would read each line of the file, but the other would not (see image).

Ibelievethatinthesecondfileitisnotidentifyinglinebreakssinceonlyonelineisreturnedwithalldata.ThesefilesareissuedbySPCBRAZILiethereisnopossibilitytochangethedefault.

Doesanyonehaveanyideahowtogetittoreadthesecondfilecorrectly?Q.:Ihavepreparedanexampletoquicklytestwithsamplefiles: here

    
asked by anonymous 21.11.2016 / 21:31

2 answers

0

Try using:

<?php

extrairDadosNotificacao("arquivo1.txt");
extrairDadosNotificacao("arquivo2.txt");

function extrairDadosNotificacao($NomeArquivo){
      $arquivo = fopen($NomeArquivo, 'r');


      while (($buffer = fgets($arquivo, 4096)) !== false) {
          echo $buffer . "<br>";
      }
}

Explaining: When you do not enter the second parameter in fgets, it reads the file to the end.

In this case, fget will read up to the "first line break" and at most 8kb (per line).

    
22.11.2016 / 18:50
-1

You have a coding problem in the second file. Or it's on utf8 and your page on it or vice versa. Use:

$linha = utf8_encode(fgets($f));

Or

$linha = utf8_decode(fgets($f));
    
22.11.2016 / 01:53