1

I am developing a website in PHP, and I must include in the index the first 3 lines of a text file in PHP. How can I do that?

<?php
$file = file_get_contents("text.txt");
//echo the first 3 lines, but it's wrong
echo $file;
?>
  • use `file()` and get indices `0-2` – Kevin Jan 24 '15 at 01:29
  • 2
    you can get idea from there http://stackoverflow.com/questions/13246597/how-to-read-a-file-line-by-line-in-php You would get more help if you google it. – Shaiful Islam Jan 24 '15 at 01:31
  • How? can you give me the correct code? I guess it isn't very difficult, but I'm not good in opening/editing/reading files in PHP –  Jan 24 '15 at 01:32
  • There are probably a hundred questions on here that are extremely similar to this. Did you try searching the site? – elixenide Jan 24 '15 at 01:35
  • How big is the file? If it is large then you should probably avoid using `file()`. It will read the *entire file* into memory. It is a little over-kill just to get 3 lines. – Sverri M. Olsen Jan 24 '15 at 01:50

3 Answers3

11

Even more simple:

<?php
$file_data = array_slice(file('file.txt'), 0, 3);
print_r($file_data);
Paul Denisevich
  • 2,329
  • 14
  • 19
  • 4
    This reads the ENTIRE file into array elements, then isolates the first three elements. Is it concise? Yes. Is it lean/efficient? No. – mickmackusa Apr 17 '21 at 22:03
  • This works, but in my case (for exemple) i have to read a file with millions of line (4/5Gb CSV) so really not a recomended option – Zakawa Aug 10 '22 at 08:50
7

Open the file, read lines, close the file:

// Open the file for reading
$file = 'file.txt';
$fh = fopen($file, 'rb');

// Handle failure
if ($fh === false) {
    die('Could not open file: '.$file);
}
// Loop 3 times
for ($i = 0; $i < 3; $i++) {
    // Read a line
    $line = fgets($fh);

    // If a line was read then output it, otherwise
    // show an error
    if ($line !== false) {
        echo $line;
    } else {
        die('An error occurred while reading from file: '.$file);
    }
}
// Close the file handle; when you are done using a
// resource you should always close it immediately
if (fclose($fh) === false) {
    die('Could not close file: '.$file);
}
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
4

The file() function returns the lines of a file as an array. Unless the file is huge (multiple megabytes), you can then use array_slice to get the first 3 elements of this:

$lines = file('file.txt');
$first3 = array_slice($lines, 0, 3);
echo implode('', $first3);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This reads the ENTIRE file into array elements, then isolates the first three elements. Is it concise? Yes. Is it lean/efficient? No. – mickmackusa Apr 17 '21 at 22:04
  • @mickmackusa Depends on how big the file is. If you want the first 3 lines of a 100-line file, this is fine. If it's a megabyte, there are better ways. – Barmar Apr 17 '21 at 22:10
  • _"This reads the ENTIRE file into array elements, then isolates the first three elements."_ is a fact. Is it worthwhile to worry about the overhead of this technique? That depends, yes. I am just bringing awareness to researchers that may choose the "shorter copy-pasta" which may not be ideal for their project. – mickmackusa Apr 17 '21 at 22:14
  • [Premature optimization is the root of all evil](http://c2.com/cgi/wiki?PrematureOptimization). Do whatever you feel is most natural and readable, and optimize it if it becomes a performance bottleneck. @mickmackusa – Barmar Apr 17 '21 at 22:19
  • Computers are really fast and memory is cheap. 99% of the time it's not a concern. Beginners usually won't be in the 1%. – Barmar Apr 17 '21 at 22:19
  • Again, this is about researcher awareness. Good, direct script design is a telltale differentiator between professional and amateur code. I am not saying this answer is "wrong" -- I am saying it could be less appropriate than `fget()`. Does it outperform `fget()` on a 5-line file? I don't know, maybe. As a real-life corollary, if you wanted to eat 3 peanuts, you wouldn't open up a new bag, dump the entire bag into a bowl, then pick out 3 peanuts. You'd more sensibly just shake 3 peanuts out of the bag into your hand. – mickmackusa Apr 17 '21 at 22:21
  • 1
    I've added a qualifier to the answer – Barmar Apr 17 '21 at 22:23