General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 5th, 2003, 01:02 AM
KiltedMile KiltedMile is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 2 KiltedMile User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Read particular line from txt file

This problem is killing me, and my knowledge of PHP….

I have a text file labeled “shows.radio” the contents are as follows:

2-4pm Show name 1
4-6pm Show name 2
6-8pm Show name 3
8-10pm Show name 4
10-12pm Show name 5


I would like to display one line from that text file when referenced by the time, so basically if the variable $time = 4-6pm is parsed to a script, the script will open the txt file and will display only the show tile “Show name 2” in the browser. How can this be done?

Reply With Quote
  #2  
Old December 9th, 2003, 11:52 AM
hagur hagur is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Iceland
Posts: 11 hagur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You could to something like this:

<?php
$filename = "shows.radio";
$handle = fopen ($filename, "r");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);

$time = $_GET["time"] //use $_POST["time"] if you are posting from a form to this document

//Split the content on linebreaks, I'm not sure about how that works on your platform, if it's only carriage-return or linefeed, or if it's both. If this doesnt work, try using chr(13) . chr(10)
$arrLines = explode(chr(13),$contents);
for ($i = 0; $i < count($arrLines)) {
$lineStart = substr($arrLines[$i],0,strlen($time));
if ($lineStart == $time) {
echo substr($arrLines[$i],strlen($time)+1);
}
}
?>

Note that I'm writing this code from the top of my head ... I haven't tested it. Theoretically, it should work - but there could be some silly errors in it.

This should give you some idea though.

Reply With Quote
  #3  
Old December 9th, 2003, 11:53 AM
hagur hagur is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Iceland
Posts: 11 hagur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
There were some obvious errors, which I have now fixed.

See if it works!

Reply With Quote
  #4  
Old January 6th, 2004, 11:55 AM
evan108 evan108 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: NYC
Posts: 1 evan108 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to evan108
Read a set of lines from a text file without reading the entire file

I have a large text file (5000 lines) I do not want to read the entire file.
I would like to read just a set of lines, like (41 - 80) or (81 - 120).

how can i do this

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Read particular line from txt file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT