0

I have written a php script which parses this text file

http://www.powerball.com/powerball/winnums-text.txt

Everything is good, but I wish to control the amount that is download i.e. I do not need every single result maybe max the first 5. At the moment I am downloading the entire file (which is a waste of memory / bandwidth).

I saw the fopen has a parameter which supposed to limit it but whatever value I placed in has no effect on the amount of text that is downloaded.

Can this be done? Thank you for reading.

Here is a small snippet of the code in question which is downloading the file.

<?php

$file = fopen("http://www.powerball.com/powerball/winnums-text.txt","rb");
$rows = array();

    while(!feof($file))
    {
        $line = fgets($file);
        $date = explode("Draw Date",$line);
        array_push($rows,$date[0]);

    }


fclose($file);

?>

Thanks everyone this is the code which just downloads the first row of results

   while(!feof($file))
        {
            $line = fgets($file);
            $date = explode("Draw Date",$line);
            array_push($rows,$date[0]);

            if(count($rows)>1)
            {
                break;
            }

        }
fclose($file);
user3364963
  • 397
  • 1
  • 10
  • 28

3 Answers3

1

You can break whenever you don't need more data. In this example when count($rows)>100

 while(!feof($file)) {
        $line = fgets($file);
        $date = explode("Draw Date",$line);
        array_push($rows,$date[0]);

       if (count($rows)>100)
          break;

  }
dynamic
  • 46,985
  • 55
  • 154
  • 231
  • Inside the while loop can also add `$string .= $line; $sizeInKB = number_format(strlen($string) / 1024, 2);` $sizeInKB will be hold the current amount downloaded. [Here are more calculations for different units besides KB](http://stackoverflow.com/questions/5501427/php-filesize-mb-kb-conversion) – Luke3butler Nov 10 '14 at 15:13
1

The issue is that your while condition is only met once you've read through to the end of the file. If you only want to get the first N lines you'll need to change that condition. Something like this might help get you started:

$lineCountLimit = 5;
$currentLineCount = 0;
while($currentLineCount < $lineCountLimit)
{
    $line = fgets($file);
    $date = explode("Draw Date",$line);
    array_push($rows,$date[0]);
    $currentLineCount++;

}
Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
0

Please try the following recipe to download only a part of the file, like 10 KBytes first, then split to lines and analyze them. How to partially download a remote file with cURL?

Community
  • 1
  • 1
Timothy Ha
  • 399
  • 3
  • 7