|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? ![]() |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
There were some obvious errors, which I have now fixed.
See if it works! |
|
#4
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Read particular line from txt file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|