Can I restrict how much ECHO command echos?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Holynunkenobi
Forum Newbie
Posts: 4
Joined: Wed Jul 27, 2005 5:22 pm

Can I restrict how much ECHO command echos?

Post 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;
}
}
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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`;
Holynunkenobi
Forum Newbie
Posts: 4
Joined: Wed Jul 27, 2005 5:22 pm

Post 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`;
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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... ;)
Holynunkenobi
Forum Newbie
Posts: 4
Joined: Wed Jul 27, 2005 5:22 pm

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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]; 
}
Holynunkenobi
Forum Newbie
Posts: 4
Joined: Wed Jul 27, 2005 5:22 pm

Post 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.
Post Reply