Jump to content

[SOLVED] start reading from a file at a set point...


Dragen

Recommended Posts

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

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.