yoob Posted September 15, 2006 Share Posted September 15, 2006 Hey guysCan 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. Link to comment https://forums.phpfreaks.com/topic/20829-print-specific-number-of-lines-from-a-file/ Share on other sites More sharing options...
joshi_v Posted September 15, 2006 Share Posted September 15, 2006 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 :) Link to comment https://forums.phpfreaks.com/topic/20829-print-specific-number-of-lines-from-a-file/#findComment-92187 Share on other sites More sharing options...
448191 Posted September 15, 2006 Share Posted September 15, 2006 There are many ways to do that, the best way depends on the size of the file, I think.Small file: [code]<?php$output = array_slice(file($filepath), 0, 10);?>[/code]Do whatever you want with the new array..Larger file:[code]<?php$handle = fopen("filename","r");for($i=0;$line = fgets ($handle,4096) && $i<10;$i++){ echo $line;}?>[/code]Making fgets part of the condition prevents you get errors when the file is shoter than 10 lines. Link to comment https://forums.phpfreaks.com/topic/20829-print-specific-number-of-lines-from-a-file/#findComment-92188 Share on other sites More sharing options...
yoob Posted September 15, 2006 Author Share Posted September 15, 2006 Thanks, all of these work like a charm, will keep them saved. Link to comment https://forums.phpfreaks.com/topic/20829-print-specific-number-of-lines-from-a-file/#findComment-92190 Share on other sites More sharing options...
yoob Posted September 15, 2006 Author Share Posted September 15, 2006 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:[code]<? $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]; }?>[/code]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? Link to comment https://forums.phpfreaks.com/topic/20829-print-specific-number-of-lines-from-a-file/#findComment-92211 Share on other sites More sharing options...
448191 Posted September 15, 2006 Share Posted September 15, 2006 It's because of you are 'reverse iterating' the array you created.for ($i=0;$i<10; $i++){ Link to comment https://forums.phpfreaks.com/topic/20829-print-specific-number-of-lines-from-a-file/#findComment-92219 Share on other sites More sharing options...
yoob Posted September 15, 2006 Author Share Posted September 15, 2006 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 Link to comment https://forums.phpfreaks.com/topic/20829-print-specific-number-of-lines-from-a-file/#findComment-92224 Share on other sites More sharing options...
448191 Posted September 15, 2006 Share Posted September 15, 2006 You're doing this: for ($i=count($plik); $i>= 0; $i--){ echo $plik[$i];Saying:start a loopassign $i the value of the highest index in the array I want to fetch fromecho the value of my array that has index $ikeep looping, while $i is greater or equal than 0 doing the following:for each loop, decrease $i by onefor each loop echo the value of my array that has index $iresulting 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! Link to comment https://forums.phpfreaks.com/topic/20829-print-specific-number-of-lines-from-a-file/#findComment-92241 Share on other sites More sharing options...
yoob Posted September 15, 2006 Author Share Posted September 15, 2006 Alright. I studied the php.net manuals, and everything you said, and i came up with this little thing:[code] $number = count($plik)-10; for ($i=count($plik); $i>=$number ; $i--){ echo $plik[$i]; }[/code]And it works ^______^Thanks a lot. Link to comment https://forums.phpfreaks.com/topic/20829-print-specific-number-of-lines-from-a-file/#findComment-92263 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.