Click to See Complete Forum and Search --> : Help with write to file.


nexxnoodlz
07-25-2005, 06:04 PM
Ages ago my mates and I setup a three word thread project to our website. Over time we added different variations to it on different pages, but decided to have the first one as the main one.

So the main page shows the data.txt but we wanna change it so that the main page shows the last few lines of all our other thread can this be done easily?

Below is the bit in our php file that opens the whole data.txt file (if its any help)

<?php
$file_loc = "data.txt";
$file = fopen($file_loc, "r");
if(!$file)
{
echo "<p>Error: Unable to open $file_loc.</p>";
}
else
{
while(!feof ($file))
{
$line = fgets($file, 1024);
echo $line;
}
}
?>

CompGeek01
07-25-2005, 07:02 PM
You could try using file() which will get the entire file into an array, then you could start wherever you wanted... third to last line..etc.


$lines = file('http://www.example.com/data.txt');
echo $lines[count($lines]-4];

nexxnoodlz
07-26-2005, 12:30 PM
Ok so I gave that a try, first came parser error so I changed ] to )
which sorted the error out but it dont seem to work all this does is omit the data.txt

Any ideas?

nexxnoodlz
07-30-2005, 01:38 PM
Right I've go it all sorted now for showing the last 10 words of the data.txt by using the following

<?php
$return_words = 10;
$file_loc = "data.txt";
$file = fopen($file_loc, "r");
if(!$file)
{
echo "<p>Error: Unable to open $file_loc.</p>";
}
else
{
while(!feof ($file))
{
$line = fgets($file);
$count1 = str_word_count($line);
$array1 = str_word_count($line,1);
$count2 = $count1 - $return_words;
while($count2 <= $count1)
{
echo $array1[$count2] ." ";
$count2 = $count2 + 1;
}
}
}
?>

How do I now modify this so it would only return the words in between the last two <br> points

This is because one of the threads is a quote one so each quote is separated by <br> so if it only returns words between the last two <br> is ensures that the last quote is shown no matter how many words is in it.

BeachSide
07-30-2005, 01:58 PM
you could use preg_split and a regular expression that says <br> is at the beginning and at the end using ^ for the beginning and $ for the end