Page 1 of 1

Can I restrict how much ECHO command echos?

Posted: Wed Jul 27, 2005 5:26 pm
by Holynunkenobi
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 (words submitted to data.txt via writetofile) 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 echos the data.txt file (if its any help)

Code: Select all

<?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;
}
}
?>

Posted: Wed Jul 27, 2005 5:35 pm
by timvw
Well, you could use fseek to navigate through the file..

I keep it simple and use the "tail" command..

Code: Select all

$lines = `tail -n $number_of_lines $file`;

Posted: Wed Jul 27, 2005 5:57 pm
by Holynunkenobi
Yeah I like the idea of keeping it simple lol
Someone suggested doing something like this..

Code: Select all

$lines = file('http://www.example.com/data.txt');
echo $lines[count($lines)-4];
but I'm feeling special at the moment and cant figure out where to put the $lines bit!

timvw wrote:I keep it simple and use the "tail" command..

Code: Select all

$lines = `tail -n $number_of_lines $file`;

Posted: Wed Jul 27, 2005 6:26 pm
by Chris Corbyn

Code: Select all

<?php
$file_loc = "data.txt";
$lines = file($file_loc);
if(!$lines)
{
    echo "<p>Error: Unable to open $file_loc.</p>";
}
else
{
    /*
     Change 10 and 4 below to get what you need 
     */
    for ($i=10; $i<=count($lines)-4; $i++)
    {
       echo $lines[$i];
    }
}
?>
That method someone gave you was half there but you need to loop still... ;)

Posted: Wed Jul 27, 2005 7:15 pm
by Holynunkenobi
Cheers guys thats what i'm after.

But unfortunately I've had a look at the data.txt file and its all on one line! So it dont show unless I change both values to zero. Even tried it on another txt file where all words are separated by <br> but still no luck!

So should I be looking at a character count (show for example last 50 characters) to get round it. But then I suppose theres always going to be occasions where words cutoff half way through.

Posted: Wed Jul 27, 2005 8:39 pm
by timvw
If you use explode on the file with "<br>" with separator you could recieve an array with words..

Code: Select all

$words = explode("<br>", file_get_contents('filewithbr.txt'));
$wordcount = count($words);

for ($i = 10; $i > 1; --$i)
{
  echo $words[$wordcount - $i]; 
}

Posted: Fri Jul 29, 2005 2:52 pm
by Holynunkenobi
Right I've go it all sorted now for only showing the last 10 words of the data by using the following

Code: Select all

<?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 change it so it would only return the words in between the last two &lt;br&gt;

This is because one of the threads is a quote one so each quote has &lt;br&gt; at the end, so if it only returns words between the last two &lt;br&gt; is ensures that a complete sentence is shown.