Dragen Posted April 27, 2007 Share Posted April 27, 2007 Hi, I' just getting into understanding how to read, write etc to files. Could someone point me in the right direction on how to open a file and start reading from a certain spot and stop reading at another place. For instance start reading at: #startitem and stop at #enditem This is the code I've got for reading the file so far: <?php $file = "/help/helpitem.txt"; $fh = @fopen($file, 'r'); if(!$fh){ $description = "Error: Can't find item text"; }else{ $description = fread($fh, filesize($file)); fclose($fh); } ?> Thanks Link to comment https://forums.phpfreaks.com/topic/48910-solved-start-reading-from-a-file-at-a-set-point/ Share on other sites More sharing options...
Dragen Posted April 27, 2007 Author Share Posted April 27, 2007 nevermind, I've found it out. Here's my code: <?php $file = "help.txt"; // opens the txt file for the item. $fh = @fopen($file, 'r'); if(!$fh){ $description = "Can't find item text"; }else{ $contents = fread($fh, filesize($file)); // sets the file as $contents fclose($fh); $delimiter = "#".$helpitm; //sets what divides each section $description = explode($delimiter, $contents); then explodes the contents using the delimiter and saves it as $description } ?> it seems to work fine, although to display it correctly I have to use: echo $description['1']; where I thought the 1 should be a 0. If I use 0 it displays nothing... also you must have the whatever your delimeter is above and below what you want to output. for example: #section1 my text here #section1 #section2 my text here #section2 etc Just thought I'd post it for anyone else looking! Link to comment https://forums.phpfreaks.com/topic/48910-solved-start-reading-from-a-file-at-a-set-point/#findComment-239709 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.