1

I am trying to read data from a comma separated text file called test.txt using PHP. The file contents is as follows:

Sel,10,5,4
Tati,5,6,7
Vinny,10,10,10

I tried to read these values using a loop with fgets(), which is used to print line by line and fgets() and explode(), which are supposed to write element per element in each line.

<?php
    $file=fopen('test.txt','r');

    if(!$file)
    {
        echo "Error reading file";
        die;
    }

    echo"Reading Line by Line";

    while(!feof($file))
    {
          $pointer=fgets($file,4096);
          echo "<p>".$pointer."<p>";    
    }

    rewind($file);

    echo "Reading value by value per line";

    while(!feof($file))
    {
          $pointer=fgets($file,4096);
          $pieces = explode(",", $pointer);
          echo"<hr>";
          echo "<p> Name:".$pieces[0]."<p>";
          echo "<p> Grade 1:".$pieces[1]."<p>";
          echo "<p> Grade 2:".$pieces[2]."<p>";
          echo "<p> Grade 3:".$pieces[3]."<p>";
          echo"<hr>";
    }   
    fclose($file);
?>

However, when I use fgets() and explode() functions in php there is a line printed with no values. This is strange, as I have only three lines in my text file and when I use fgets() without explode() it returns the values in the correct number of lines.

What is wrong in this code?

Edit: here is my code

3
  • 2
    is the line with no values at the beginning or end of your result? Is it possible you have a line break at the end of your text file? Commented Apr 1, 2016 at 16:08
  • 2
    If you use SPLFileObject and treat this csv file as a csv file, you have options too ignore empty lines Commented Apr 1, 2016 at 16:10
  • @Sean it is at the end of the file. Commented Apr 1, 2016 at 16:23

1 Answer 1

1

@Mad i test your code and got that problem is because of <p> tag is not closed inside loop, So close them and your problem will solve:-

<?php
$file=fopen('test.txt','r');

if(!$file)
{
    echo "Error reading file";
    die;
}

echo"Reading Line by Line";

while(!feof($file))
{
      $pointer=fgets($file,4096);
      if(strlen($pointer) !==0){
        echo "<p>".$pointer."</p>";
      }       
}

rewind($file);

echo "Reading value by value per line";

while(!feof($file))
{
      $pointer=fgets($file,4096);
      $pieces = explode(",", $pointer);
      if(strlen($pointer) !==0){
          echo"<hr>";
          echo "<p> Name:".$pieces[0]."</p>";
          echo "<p> Grade 1:".$pieces[1]."</p>";
          echo "<p> Grade 2:".$pieces[2]."</p>";
          echo "<p> Grade 3:".$pieces[3]."</p>";
          echo"<hr>";
      }
}   
fclose($file);
?>

Output:- at my local PC:- http://prntscr.com/amufst

Note:- my text file have no empty lines in starting as well as ending.

Sign up to request clarification or add additional context in comments.

7 Comments

I repeated your code and it is still not working. I will check my cache.
How do I check if there are empty lines in my text file?
Here is the console output: Reading Line by Line<p>Sel,10,5,4 </p><p>Tati,5,6,7 </p><p>Vinny,10,10,10 </p><p></p>Reading value by value per line<hr><p> Name:Sel</p><p> Grade 1:10</p><p> Grade 2:5</p><p> Grade 3:4 </p><hr><hr><p> Name:Tati</p><p> Grade 1:5</p><p> Grade 2:6</p><p> Grade 3:7 </p><hr><hr><p> Name:Vinny</p><p> Grade 1:10</p><p> Grade 2:10</p><p> Grade 3:10 </p><hr><hr><p> Name:</p><p> Grade 1:</p><p> Grade 2:</p><p> Grade 3:</p><hr>
lot of possibility:- $pieces = explode(",", $pointer); if(count($pieces) <=0){empty lines} or $pointer=fgets($file,4096);if(strlen($pointer) ==0){empty line}
Take my code, remove empty lines from text file, and check first by clearing cache of your browser.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.