Page 1 of 1
How to tell php to begin reading a file at a particular line
Posted: Mon Nov 03, 2003 2:20 pm
by infolock
Hello. I am currently just learning php so i decided to write a log parser. I've asked a lot of questions and have gotten great results from the users here. Now, I have a new question
My log file has data at the beginning of it that i really don't need to parse. the 4th line starts where the headers are, and then the 5th line consists of the actual values. How do i tell php to read the 4th line for only the headers, and then read from lines 5 to EOF for t he values ?
Thanks

Posted: Mon Nov 03, 2003 3:25 pm
by twigletmac
If you're using file then you can do something like:
Code: Select all
$file_contents = file($path_to_file);
foreach ($file_contents as $num => $line_data) {
$line_no = $num + 1;
if ($line_no == 4) {
// do header stuff
} elseif ($line_no > 4) {
// do main content stuff
}
}
Mac
Posted: Mon Nov 03, 2003 3:29 pm
by infolock
Thanks mac
btw, how do you make your code look color coated instead of mono? Inquiring minds

Posted: Mon Nov 03, 2003 3:35 pm
by DuFF
click the PHP button right above replys, paste your code and hit it again. Alternatively you could just put [ php] and [ /php] without the spaces.
Posted: Mon Nov 03, 2003 3:36 pm
by twigletmac
Use the PHP BBCode tags, put your code between
tags instead of code ones.
Mac
Posted: Mon Nov 03, 2003 3:51 pm
by infolock
sweet thanks
Posted: Tue Nov 04, 2003 1:24 pm
by JAM
Also, reading a file into an array will give you other usefull things to work with:
Code: Select all
clean.txt contains:
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6
Code: Select all
<pre>
<?php
$startat = 3;
$array = file('clean.txt');
foreach ($array as $row => $value) {
if ($row > $startat-1) { // as arrays start at 0, this is easier to 'read'
echo $value;
}
}
?>