Page 1 of 1

How read Alternative lines from a TXT files

Posted: Wed Oct 08, 2008 1:56 am
by hmvrulz
am new to File function in php

I have a list in a txt file.. i wanna capture only alternative lines from the txt file.

Any suggestions. Thanx in advance

Re: How read Alternative lines from a TXT files

Posted: Wed Oct 08, 2008 3:00 am
by mchaggis
The simplest would be to just read two lines at a time, eg:

Code: Select all

 
<?php
$oddLines = $evenLines = array();
$fp = fopen('myfile.txt','r'(;
while( ($line = fgets($fp, 2048)) ) {
        $oddLines[] = $line;
        $evenLines[] = = fgets($fp, 2048);
}
fclose($fp);
 
// $oddLines now contains lines 1,3,5...
// $evenLines now contains lines 2,4,6...
?>
 
Hope this helps

Re: How read Alternative lines from a TXT files

Posted: Wed Oct 08, 2008 4:16 am
by hmvrulz
mchaggis wrote:The simplest would be to just read two lines at a time, eg:

Code: Select all

 
<?php
$oddLines = $evenLines = array();
$fp = fopen('myfile.txt','r'(;
while( ($line = fgets($fp, 2048)) ) {
        $oddLines[] = $line;
        $evenLines[] = = fgets($fp, 2048);
}
fclose($fp);
 
// $oddLines now contains lines 1,3,5...
// $evenLines now contains lines 2,4,6...
?>
 

Hope this helps
Thanx