0

I am trying to read the first 5 line code-block in txt file, please how do i do this

I have this php code to get only the first line

<?php
$file = 'example.txt';
$f = fopen($file, 'r');
$line = fgets($f);
    while (($line = fgets( $f)) !== false) {
            for ($list = 1; $list < 6; $list++){
                $codeline= htmlentities($line );
            }
        }   
fclose($f); 
?>

3 Answers3

2

You can use a for loop:

for ($x = 1; $x < 6; $x++) {
    $line = fgets($f);
}
Panda
  • 6,955
  • 6
  • 40
  • 55
0

Even more simple:

<?php
$file_data = array_slice(file('file.txt'), 0, 5);
print_r($file_data);

source: get the first 3 lines of a text file in php from @paul-denisevich

Community
  • 1
  • 1
Beginner
  • 4,118
  • 3
  • 17
  • 26
0

To open and read a file line by line:

$file = fopen( "/path/to/file.txt", "r" );

$index=0;
while ((( $line = fgets( $file )) !== false) && ( $index++ < 5 )) {

    echo $line;
}

fclose( $file );

Here, I am initializing a variable index to 0.

In the while loop, we will use fgets to read the next line of the file, and assign it to the variable line. We will also check that the value of index is less then 5, our desired line count, in addition to incrementing the value of the index, after we have read the line.

Once the value of index has reached > 5, the loop will exit, and the file stream will be closed.

The advantage of using fopen and fgets over something like file, is that the latter will load the entire contents of the file into memory - even if you do not plan on using the whole thing.

With a multi line file, the above code will print out the first five lines.

This is a multi line

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • my problem is `doSomethingWith` am new in php – Calis Godswill Dec 26 '16 at 03:11
  • That is just a function name that I have created, this is where you will preform whatever you need to do with each line. I have added in the counter as well. – Matt Clark Dec 26 '16 at 03:15
  • Can you explain `what` is not working? Any errors? Give me 5 to try it out on a webserver - this was typed up from memory. – Matt Clark Dec 26 '16 at 03:22
  • Does the user running the webserver, `apache`, have permission to open the file? – Matt Clark Dec 26 '16 at 03:23
  • Is not showing error it only show the fifth line in the file but i want it to print 1 to 5 only – Calis Godswill Dec 26 '16 at 03:25
  • `it only show the fifth line` - what have you changed for the function? I have edited the above to just `echo` the line. There are two links now, one to a multi line file that I am using to test, and one to the PHP code that I have posted. It appears to work as I would expect. – Matt Clark Dec 26 '16 at 03:30
  • is not working for me i don't know what the problem is but what should i do now your example is working should i mark as accep? – Calis Godswill Dec 26 '16 at 03:45
  • 1
    I am sorry that it is not working, I would like for it to before you accept. Is the code posted in your question completely up to date? If not, can you update it and we can work trough it. [This is the code as it exists on my server.](https://www.mclarkdev.com/test/file.notphp). – Matt Clark Dec 26 '16 at 03:48