Bricktop Posted August 18, 2009 Share Posted August 18, 2009 Hi all, I have a bit of code which reads through the lines of a text file and outputs the result, the code is fine and is as follows: function showprogress() { $progressfile = text.txt; $content = ""; foreach(file($progressfile) as $line) { list($percentcomplete,$status) = explode("||", $line, 2); $content .= '<div class="progressbar" style="width:'.$percentcomplete.'%">'.$percentcomplete.'%</div>'; $content .= '<div class="statusupdate">'.$status.'</div>'; } echo $content; } What I would like to do is limit the output to only the last entry in the file, so instead of every line being output only the last line in the file is output. Any ideas? Thanks Link to comment https://forums.phpfreaks.com/topic/170809-solved-limit-by-1/ Share on other sites More sharing options...
DEVILofDARKNESS Posted August 18, 2009 Share Posted August 18, 2009 After googling I found this answer: $file = "/path/to/file"; $data = file($file); $line = $data[count($data)-1]; Though, this WILL load the whole file into memory. Possibly a problem (or not). A better solution is this: $file = escapeshellarg($file); // for the security concious (should be everyone!) $line = `tail -n 1 $file`; Link to comment https://forums.phpfreaks.com/topic/170809-solved-limit-by-1/#findComment-900812 Share on other sites More sharing options...
Prismatic Posted August 18, 2009 Share Posted August 18, 2009 <?php function showprogress() { $progressfile = "text.txt"; $content = null; $arr = file($progressfile); $count = count($arr); list($percentcomplete, $status) = explode("||", $arr[$count], 2); $content .= '<div class="progressbar" style="width:'.$percentcomplete.'%">'.$percentcomplete.'%</div>'; $content .= '<div class="statusupdate">'.$status.'</div>'; echo $content; } ?> Link to comment https://forums.phpfreaks.com/topic/170809-solved-limit-by-1/#findComment-900813 Share on other sites More sharing options...
Bricktop Posted August 18, 2009 Author Share Posted August 18, 2009 Thanks guys, I can't get either to work, I'm not getting any values output. here is what I have: //This is the countdown.txt file, the countdown values are stored in this file $progressfile = "text.txt"; $content = null; $arr = file($progressfile); $count = count($arr); list($percentcomplete, $status) = explode("||", $arr[$count], 2); $content .= '<div class="progressbar" style="width:'.$percentcomplete.'%">'.$percentcomplete.'%</div>'; $content .= '<div class="statusupdate">'.$status.'</div>'; echo $content; Link to comment https://forums.phpfreaks.com/topic/170809-solved-limit-by-1/#findComment-900853 Share on other sites More sharing options...
Bricktop Posted August 18, 2009 Author Share Posted August 18, 2009 I've worked it out, for anyone with a similar problem I used: $lines = file("text.txt"); $last = sizeof($lines) - 1 ; list($percentcomplete, $status) = explode("||", $lines[$last], 2); Thanks. Link to comment https://forums.phpfreaks.com/topic/170809-solved-limit-by-1/#findComment-900864 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.