Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-19-2003, 04:00 PM   PM User | #1
scroots
Senior Coder

 
Join Date: Jun 2002
Location: UK
Posts: 1,137
Thanks: 0
Thanked 0 Times in 0 Posts
scroots is an unknown quantity at this point
textfile read specified line(s)

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
__________________
Spammers next time you spam me consider the implications:
(1) that you will be persuaded by me(in a legitimate mannor)
(2)It is worthless to you, when i have finished
scroots is offline   Reply With Quote
Old 04-19-2003, 04:34 PM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
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.
raf is offline   Reply With Quote
Old 04-19-2003, 05:57 PM   PM User | #3
scroots
Senior Coder

 
Join Date: Jun 2002
Location: UK
Posts: 1,137
Thanks: 0
Thanked 0 Times in 0 Posts
scroots is an unknown quantity at this point
do you have any code for this? as i'm a little bit confused as i'm new to php.

scroots
__________________
Spammers next time you spam me consider the implications:
(1) that you will be persuaded by me(in a legitimate mannor)
(2)It is worthless to you, when i have finished
scroots is offline   Reply With Quote
Old 04-19-2003, 06:13 PM   PM User | #4
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
<edit>filefunction (see below) is probably more efficinient way to get a speciffic line</edit>


untested code
PHP Code:
<?php
$line 
0;
$filehand fopen ("thefile.txt""r");
while (!
feof ($filehand)) {
  if (
$line == 5) {
    echo 
fgets($filehand4096);
    break;
  }
  else {
    
$line ++
  }
}
fclose ($filehand);
?>
more info on php.net
http://www.php.net/manual/en/function.fgets.php

Last edited by raf; 04-20-2003 at 08:22 AM..
raf is offline   Reply With Quote
Old 04-19-2003, 10:22 PM   PM User | #5
scroots
Senior Coder

 
Join Date: Jun 2002
Location: UK
Posts: 1,137
Thanks: 0
Thanked 0 Times in 0 Posts
scroots is an unknown quantity at this point
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
__________________
Spammers next time you spam me consider the implications:
(1) that you will be persuaded by me(in a legitimate mannor)
(2)It is worthless to you, when i have finished
scroots is offline   Reply With Quote
Old 04-20-2003, 02:53 AM   PM User | #6
duniyadnd
Regular Coder

 
Join Date: Jun 2002
Location: Depends on the time of year
Posts: 478
Thanks: 0
Thanked 0 Times in 0 Posts
duniyadnd is an unknown quantity at this point
Look at the "file" function. It converts each line as a seperate element in an array (in this case $array)


PHP Code:
$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
duniyadnd is offline   Reply With Quote
Old 04-20-2003, 10:00 AM   PM User | #7
scroots
Senior Coder

 
Join Date: Jun 2002
Location: UK
Posts: 1,137
Thanks: 0
Thanked 0 Times in 0 Posts
scroots is an unknown quantity at this point
duniyadnd thanks for that it could prove very useful later.

scroots
__________________
Spammers next time you spam me consider the implications:
(1) that you will be persuaded by me(in a legitimate mannor)
(2)It is worthless to you, when i have finished
scroots is offline   Reply With Quote
Old 04-22-2003, 08:49 PM   PM User | #8
scroots
Senior Coder

 
Join Date: Jun 2002
Location: UK
Posts: 1,137
Thanks: 0
Thanked 0 Times in 0 Posts
scroots is an unknown quantity at this point
Smile

I have decided to use the following code
PHP 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
__________________
Spammers next time you spam me consider the implications:
(1) that you will be persuaded by me(in a legitimate mannor)
(2)It is worthless to you, when i have finished

Last edited by scroots; 04-22-2003 at 08:54 PM..
scroots is offline   Reply With Quote
Old 04-22-2003, 09:03 PM   PM User | #9
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
function feof( resource handle) does that, but so does this
while (!feof ($filehand))
raf is offline   Reply With Quote
Old 04-22-2003, 10:18 PM   PM User | #10
weronpc
Regular Coder

 
Join Date: Apr 2003
Location: Canada, Ontario, Mississauga
Posts: 312
Thanks: 0
Thanked 0 Times in 0 Posts
weronpc is an unknown quantity at this point
Try mysql?

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
weronpc is offline   Reply With Quote
Old 04-23-2003, 09:56 AM   PM User | #11
Weirdan
New Coder

 
Join Date: Mar 2003
Location: Somewhere far beyond
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
Weirdan is an unknown quantity at this point
Quote:
Originally posted by scroots
I have decided to use the following code
PHP 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:
PHP Code:
$array file($file_name);
for(
$line=0;$line<sizeof($array);$line++);
echo 
$array[$line]; 
Also, if you want to print last line only:
PHP Code:
$array file($file_name);
echo 
$array[sizeof($array)-1]; 
__________________
WBR, Weirdan.
Weirdan is offline   Reply With Quote
Old 04-23-2003, 06:09 PM   PM User | #12
scroots
Senior Coder

 
Join Date: Jun 2002
Location: UK
Posts: 1,137
Thanks: 0
Thanked 0 Times in 0 Posts
scroots is an unknown quantity at this point
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
__________________
Spammers next time you spam me consider the implications:
(1) that you will be persuaded by me(in a legitimate mannor)
(2)It is worthless to you, when i have finished
scroots is offline   Reply With Quote
Old 04-23-2003, 06:23 PM   PM User | #13
duniyadnd
Regular Coder

 
Join Date: Jun 2002
Location: Depends on the time of year
Posts: 478
Thanks: 0
Thanked 0 Times in 0 Posts
duniyadnd is an unknown quantity at this point
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
duniyadnd is offline   Reply With Quote
Old 04-23-2003, 09:31 PM   PM User | #14
scroots
Senior Coder

 
Join Date: Jun 2002
Location: UK
Posts: 1,137
Thanks: 0
Thanked 0 Times in 0 Posts
scroots is an unknown quantity at this point
thanks very much.

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

scroots
__________________
Spammers next time you spam me consider the implications:
(1) that you will be persuaded by me(in a legitimate mannor)
(2)It is worthless to you, when i have finished
scroots is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:44 AM.


Advertisement
Log in to turn off these ads.