skip first line

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
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

skip first line

Post by dannyd »

Hi,

How Can I ignore the first line when I'm reading a file. I need it to work with my code below.



Below is the code that goes through the files.

Code: Select all

//parse contents in file
             $file_handle = file_get_contents('./logs/' . $filename);
              // Explodes .txt file into lines (\n)
              $Lines = explode("\n",$file_handle);
 
                  //display fields from file     
                    foreach($Lines as $Line) {
                    
                    $Line = preg_replace('/\s\s+/', ' ', trim($Line));
                    $fields = explode(" ",$Line);
                     
                    echo $fields[1] . '&nbsp;' . $fields[2] . '&nbsp;' . $fields[3] . '<BR>';
      
                   }
 
}
Thanks!
Last edited by John Cartwright on Fri Feb 15, 2008 3:47 pm, edited 1 time in total.
Reason: Please use [code=php][/code] tags when posting code!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: skip first line

Post by John Cartwright »

Code: Select all

$Lines = explode("\n",$file_handle);
array_shift($Lines);
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: skip first line

Post by liljester »

try the next() function.

Code: Select all

//parse contents in file
$file_handle = file_get_contents('./logs/' . $filename);
// Explodes .txt file into lines (\n)
$Lines = explode("\n",$file_handle);
next($Lines);
//display fields from file 
foreach($Lines as $Line) {
 
$Line = preg_replace('/\s\s+/', ' ', trim($Line));
$fields = explode(" ",$Line);
 
echo $fields[1] . '&nbsp;' . $fields[2] . '&nbsp;' . $fields[3] . '<BR>';
 
}
 
}
Post Reply