...

View Full Version : textfile read specified line(s)



scroots
04-19-2003, 04:00 PM
how using php can i read a specified line/lines of a text file. an example i would like to read lines 1-4 and output them to a page. another example would be read line 5 and output it to a page.

any ideas?

scroots

raf
04-19-2003, 04:34 PM
with fopen() and fgets() (or fread() if you develop cross-platform stuff). fgets read one line, so you can have a loop until the pointer is on the line you need.

scroots
04-19-2003, 05:57 PM
do you have any code for this? as i'm a little bit confused as i'm new to php.

scroots

raf
04-19-2003, 06:13 PM
<edit>filefunction (see below) is probably more efficinient way to get a speciffic line</edit>


untested code


<?php
$line = 0;
$filehand = fopen ("thefile.txt", "r");
while (!feof ($filehand)) {
if ($line == 5) {
echo fgets($filehand, 4096);
break;
}
else {
$line ++
}
}
fclose ($filehand);
?>


more info on php.net
http://www.php.net/manual/en/function.fgets.php

scroots
04-19-2003, 10:22 PM
thanks raf your code explains it a lot better. I did read the manual section on it earlier and that was what left me a little confused.

thanks and happy easter.
scroots

duniyadnd
04-20-2003, 02:53 AM
Look at the "file" function. It converts each line as a seperate element in an array (in this case $array)




$array = file($file_name);
echo $array[0]; //first line
echo $array[1]; //second line
echo $array[2]; //third line
echo $array[3]; //fourth line


Those should be your first four lines.


Later
Duniyadnd

scroots
04-20-2003, 10:00 AM
duniyadnd thanks for that it could prove very useful later.

scroots

scroots
04-22-2003, 08:49 PM
I have decided to use the following code


$line == 0;
$array = file($file_name);
//need a loop system while not end of file
$line++;
echo $array[$line];


if i use while or another method how can i detect the end of the file? so that htere are no more lines left to read?

which is the correct syntax to use as a loop with the condition that its not the end of the file? to go in the space of the cooment.
scroots

raf
04-22-2003, 09:03 PM
function feof( resource handle) does that, but so does this
while (!feof ($filehand))
;)

weronpc
04-22-2003, 10:18 PM
I think you should try mysql..

I am willing to teach you if you want. just go on msn and I will teach you step by step.

I am offering you this cuz I was a newbie once, it was hard...

let me know if you want to..

Mike

Weirdan
04-23-2003, 09:56 AM
Originally posted by scroots
I have decided to use the following code


$line == 0;
$array = file($file_name);
//need a loop system while not end of file
$line++;
echo $array[$line];


if i use while or another method how can i detect the end of the file? so that htere are no more lines left to read?

which is the correct syntax to use as a loop with the condition that its not the end of the file? to go in the space of the cooment.
scroots
You are looping through the array, not the file.
So:


$array = file($file_name);
for($line=0;$line<sizeof($array);$line++);
echo $array[$line];

Also, if you want to print last line only:


$array = file($file_name);
echo $array[sizeof($array)-1];

scroots
04-23-2003, 06:09 PM
suppose so.

I will try it out, but wouldn't an error be produced if it looks for part of the array that doesn't exist? e.g. it looks for arrayline number nine in an eight lined text file.

scroots

duniyadnd
04-23-2003, 06:23 PM
No error, as it looks at the last element minus one. You're not looking at the file, you looking at the array, so you only concerned with the array length, which you got already, by using the sizeof function (personally, i use the count function, but they both get the job done).

Duniyadnd

scroots
04-23-2003, 09:31 PM
thanks very much.

it will prove very useful and it has solved my current problem.

scroots



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum