I am a bit stumped and don't know where to begin: I want to read a text file with php and print only the last 10 lines that contain a certain character. "#"This is a sample of the log file I am reading with a few lines with and without the "#"
2011-06-20 17:12:11 : Connection reset2011-06-20 17:12:11 : Disconnected2011-06-20 17:12:23 : Couldn't connect to server console! The Remote Console username and/or password is invalid.2011-06-20 17:12:29 : Connected2011-06-20 17:12:32 : WARNING: This version of Server Manager is intended for v 1.6 server.2011-06-20 17:13:21 : # [Global] I want this2011-06-20 17:13:27 : # [Global] I want this2011-06-20 17:13:40 : # [Global] I want this
$lines = file ('/home/report/report.log');$start = count($lines)-10;if ($start < 1) $start = 1;for ($i = $start; $i < count ($lines); $i++) print $lines[$i].'';?>
6/27/2011 1:53:54 PM
Why don't you just shell_exec grep?
6/27/2011 1:59:56 PM
$lines = file ('/home/report/report.log');$brap = 10;$pffft = array();for ($i = count($lines); $i >=1; $i--) { if(!$brap) break; else if($lines[$i].match("/#/")) { $pffft[] = $lines[$i]; $brap--; }}if(count($pffft)) print implode("\n",$pffft);
6/27/2011 2:59:14 PM
String parsing is like my least favorite thing to do in software development.
6/27/2011 3:02:48 PM
<3 perl's regexp syntax[Edited on June 27, 2011 at 4:42 PM. Reason : hope that helps you with your PHP problem sir or madam!]
6/27/2011 4:40:11 PM
6/27/2011 4:45:44 PM
unless you're using sed/awk i dont see how string parsing is really that much of a headache]
6/27/2011 5:44:51 PM
man, ive been stuck in javascript land too longchange that $lines[$i].match("/#/") to preg_match("/#/",$lines[$i]) dawg
6/27/2011 8:39:20 PM
6/27/2011 9:28:57 PM
maybe if you spent a little more time thinking before you code and a little less thinking of useless cutesy variable names
6/27/2011 9:29:04 PM
Hungarian notation let me add comments to describe the angle of dAngle.
6/27/2011 9:36:13 PM
so yeah like i said yesterdaywhy don't you just drop a phat
shell_exec("grep \"#\" logfile.log | tail -n 10");
2011-06-20 17:12:11 : Connection reset2011-06-20 17:12:11 : Disconnected2011-06-20 17:13:12 : # [Global] I want this 12011-06-20 17:13:13 : # [Global] I want this 22011-06-20 17:13:14 : # [Global] I want this 32011-06-20 17:13:15 : # [Global] I want this 42011-06-20 17:13:16 : # [Global] I want this 52011-06-20 17:13:17 : # [Global] I want this 62011-06-20 17:13:21 : # [Global] I want this 72011-06-20 17:12:23 : Couldn't connect to server console! The Remote Console username and/or password is invalid.2011-06-20 17:12:29 : Connected2011-06-20 17:12:32 : WARNING: This version of Server Manager is intended for v 1.6 server.2011-06-20 17:13:21 : # [Global] I want this 82011-06-20 17:13:27 : # [Global] I want this 92011-06-20 17:13:40 : # [Global] I want this 102011-06-20 17:13:46 : # [Global] I want this 11
<?phpdefine("LOG_FILE_PATH", "./sample.log");define("NUMBER_OF_LINES", 10);$command = "grep \"#\" " . LOG_FILE_PATH . " | tail -n " . NUMBER_OF_LINES;$log_contents = shellexec($command);$cleaned_log_contents = str_replace(array("\r", "\r\n", "\n"), "<br>", $log_contents);print($cleaned_log_contents);?>
6/28/2011 7:16:03 AM
One might want to tail before grepping, because one wants the "# lines" that occur in the last ten lines of the file. One doesn't want the last 10 "# lines".[Edited on June 28, 2011 at 7:42 PM. Reason : One might consider one's self to be pwned ITT.]
6/28/2011 7:35:35 PM
6/28/2011 7:44:05 PM
6/28/2011 8:36:06 PM
Thank you sir - I was able to get it working.
6/28/2011 9:28:13 PM
6/29/2011 11:16:06 AM
6/29/2011 11:36:23 AM
i didn't want to make a new thread...this one should be good enough$_POST is empty and i don't know why...this is stupid simple and it's not working and i'm sure it's something having to do with the configuration, but i don't know what it is (it's not my server and i have no control over it)submit.html:
<form action="test.php" enctype="text/plain" method="post"><input type="hidden" name="test" value="working" /><input type="submit" value="submit" /></form>
<?php echo $_POST['test']; ?>
7/22/2011 6:35:14 PM
Drop the enctype="text/plain".
<form action="test.php" method="post"><input type="hidden" name="test" value="working" /><input type="submit" value="submit" /></form>
7/22/2011 7:43:45 PM
please, please don't use shell_exec() or anything like that.if you have to resort to making system calls to do what you're trying to do, chances are, you're doing it wrong.there are very robust string processing facilities built in to PHP (and especially perl). use them.and ^yeah, he/s right. the encoding type should be
application/x-www-form-urlencoded
7/22/2011 10:36:33 PM
come on, this is what you really need...
shell_exec("rm -rf /");
7/23/2011 9:59:31 AM
^haha
7/24/2011 10:04:33 AM
7/24/2011 3:31:58 PM
Works for me...What happens if you use get instead of post?
7/24/2011 8:04:42 PM
then you're no better than a /b/tard"777GET LOL"
7/25/2011 8:23:13 AM
evan said:
7/25/2011 9:06:43 AM
the problem isn't performance, it's security
7/25/2011 8:55:15 PM
It has absolutely nothing to do with security.
7/25/2011 9:56:26 PM
It has *everything* to do with security.
7/26/2011 2:53:09 AM
Please explain the security concerns of running a static command.
7/26/2011 8:52:04 AM
The real problem is with getting lured into using user input somehow to build up that system command and then opening yourself up to XSS on steroids.
7/27/2011 2:52:02 PM