How read Alternative lines from a TXT files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmvrulz
Forum Newbie
Posts: 20
Joined: Fri Oct 03, 2008 10:20 pm

How read Alternative lines from a TXT files

Post 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
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Re: How read Alternative lines from a TXT files

Post 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
hmvrulz
Forum Newbie
Posts: 20
Joined: Fri Oct 03, 2008 10:20 pm

Re: How read Alternative lines from a TXT files

Post 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
Post Reply