Jump to content


Photo

Print specific number of lines from a file.


  • Please log in to reply
8 replies to this topic

#1 yoob

yoob
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 15 September 2006 - 06:37 AM

Hey guys

Can you tell me how to make a script that would open the file, and print out only the first 10 lines from it? First 10 from the top.

#2 joshi_v

joshi_v
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 15 September 2006 - 07:00 AM

Try this!


$handle = fopen("filename","r");

for($i=0;$i<10;$i++)
{
    $str=fgets($handle,4096);
    echo  $str;

}


This will workd when u have new line characters in yur fle.


cheers :)

cheers :)



#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 15 September 2006 - 07:07 AM

There are many ways to do that, the best way depends on the size of the file, I think.

Small file:
<?php
$output = array_slice(file($filepath), 0, 10);
?>
Do whatever you want with the new array..

Larger file:
<?php
$handle = fopen("filename","r");

for($i=0;$line = fgets ($handle,4096) && $i<10;$i++){
   echo  $line;
}
?>

Making fgets part of the condition prevents you get errors when the file is shoter than 10 lines.

#4 yoob

yoob
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 15 September 2006 - 07:11 AM

Thanks, all of these work like a charm, will keep them saved.

#5 yoob

yoob
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 15 September 2006 - 07:47 AM

Hmm, another problem.

See, i have this small piece of line reverse code, making the bottom lines of the file show up at the top. I've put it together myself so it may not be perfect:

<?

        $file=fopen("news/news.txt", "r");
        flock($file,1);

        $plik= array();
        while (!feof($file)){
                $plik[] = fgets($file,5000);
         }

        flock($file,3);
        fclose($file);

         for ($i=count($plik); $i>= 0; $i--){
         echo $plik[$i];
         }
?>

I try to merge it with the code you guys provided, but i dont think im skilled enough to understand all this. Could you help me out?


#6 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 15 September 2006 - 08:01 AM

It's because of you are 'reverse iterating' the array you created.

for ($i=0;$i<10; $i++){

#7 yoob

yoob
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 15 September 2006 - 08:21 AM

I know but is there a way i could somehow merge this reverse iterating with the 'for ($i=0;$i<10; $i++){'?

I mean, where should i put it? Seems all those $i's collide with each other

#8 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 15 September 2006 - 08:37 AM

You're doing this:

for ($i=count($plik); $i>= 0; $i--){
        echo $plik[$i];

Saying:

start a loop
assign $i the value of the highest index in the array I want to fetch from
echo the value of my array that has index $i

keep looping, while $i is greater or equal than 0 doing the following:
for each loop, decrease $i by one
for each loop echo the value of my array that has index $i


resulting in, for say an array with 20 values:

$arr[20]
$arr[19]
$arr[18]
etc...

Get it?

You have to fetch the values in the same order you put them in there!

#9 yoob

yoob
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 15 September 2006 - 09:31 AM

Alright. I studied the php.net manuals, and everything you said, and i came up with this little thing:

        $number = count($plik)-10;

         for ($i=count($plik); $i>=$number ; $i--){
         echo $plik[$i];
         }

And it works ^______^

Thanks a lot.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users