0

I have large text file that contain 800 K lines size 60 MB.I tried this code

$fileData = function() {
    $file = fopen('800K.txt','r');
    if (!$file)
        die('file does not exist or cannot be opened');
    while (($line = fgets($file)) !== false) {
        yield $line;
    }
    fclose($file);
};

foreach ($fileData() as $line) {
   echo $line.'<br>';
}

It really works : ).but con is it reads all line 800 K lines :( ....... it is possible convert into for loop , so it will be easy to selected line like

for ($i=1; $i<=10000; $i++){ 
$line=trim($lines[$i]); 
}



ANSWER :
$i=0;  //// start number
foreach ($fileData() as $line) {
   if($i==100) break;  ///// end number
   echo $line.'<br>';
   $i++; 
}
  • `be easy to selected line like`. Not sure what you're asking. Could you clear up your question. – Devon Aug 05 '18 at 17:04

1 Answers1

-1

Edit:

This post has a few tests where they do this with larger files...

https://stackoverflow.com/a/31235307/2405805

$file = '../data/800k.txt';
$lines = file($file);
$rangeStart = 200000;
$rangeEnd = 300000;
$selection = array_slice($lines,$rangeStart,$rangeEnd);
echo json_encode($selection);

Or:

$file = '../data/800k.txt';
$rangeStart = 200000;
$rangeLength = 100000;
foreach (new LimitIterator($file, $rangeStart, $rangeLength) as $line) {
    array_push($lines,$line);
}
echo json_encode($lines);

Note, I'm not 100% with these methods if the item starts at 0 or 1, so it may be (200000, 300000) or (199999, 299999);

  • 2
    If you have a gigabyte of free memory for the script... – Devon Aug 05 '18 at 17:03
  • Yeah, sorry, misread the question. I'll update this in a bit with a better answer. – digitalhigh Aug 05 '18 at 17:05
  • Updated with a better answer... – digitalhigh Aug 05 '18 at 17:07
  • @Devon i have 16 GB RAM for server – meethay aam Aug 05 '18 at 17:25
  • @digitalhigh how it possible to get lines from 200K to 300K – meethay aam Aug 05 '18 at 17:26
  • If you're using the first example: `$selected = array_slice($lines, 200000, 300000);` Of for the second example: `$i = 199999; $spl->seek($i); $lines = []; while ($i <=299999) { $i++; $spl->next(); array_push($lines, $spl->current(); } // $lines will then contain all elements from 200000 to 300000. ` Note, I'm not sure if you need to start with item 199999 or 200000 with the $spl item. – digitalhigh Aug 05 '18 at 18:00
  • @meethayaam - Updated the example to provide a range selection... – digitalhigh Aug 05 '18 at 18:14
  • 1
    A more idiomatic way, when using SPL, would be to use a [LimitIterator](http://php.net/limititerator). E.g. given an SplFileObject in `$file`, you could read 100 lines from offset 12345 with a loop like: `foreach (new LimitIterator($file, 12345, 100) as $line) { ... }` – salathe Aug 05 '18 at 18:19
  • @salathe - That's pretty... – digitalhigh Aug 05 '18 at 18:21
  • @digitalhigh too much difficult solution .it is not possible to modifies my question code – meethay aam Aug 05 '18 at 19:23