Jump to content

Archived

This topic is now archived and is closed to further replies.

mrmagoo_83

Help parsing HTML log file

Recommended Posts

Ok, I know hardly anything about php code, but I have worked with it a little.

I have an html log file that is a compilation of chat events on our server.

I need to create a script that will evaluate the log file, and return entire lines that contain certain strings. We are examing the log file for rule violations.

ex.
[code]<tr><td>[08/08/2006 04:43:37]</td><td><FONT color="#254117">[P.A.K]DarkPrince</FONT></td><td><FONT COLOR="#000000">[DEFENSE] :D</FONT></td></tr>
<tr><td>[08/08/2006 04:44:18]</td><td><FONT color="#FF0000">[P.A.K]DarkPrince</FONT></td><td><FONT COLOR="#000000">[DEFENSE] :D:D:D</FONT></td></tr>
<tr><td>[08/08/2006 04:44:19]</td><td><FONT color="#254117">SentinalAtArms</FONT></td><td><FONT COLOR="#000000">[ASSAULT] :D</FONT></td></tr>
<tr><td>[08/08/2006 04:45:28]</td><td><FONT color="#FF0000">[P.A.K]DarkPrince</FONT></td><td><FONT COLOR="#000000">[DEFENSE] camper:(</FONT></td></tr>[/code]

I want the script to run such that if I search for "Dark" I would get the return of:
[code]<tr><td>[08/08/2006 04:43:37]</td><td><FONT color="#254117">[P.A.K]DarkPrince</FONT></td><td><FONT COLOR="#000000">[DEFENSE] :D</FONT></td></tr>
<tr><td>[08/08/2006 04:44:18]</td><td><FONT color="#FF0000">[P.A.K]DarkPrince</FONT></td><td><FONT COLOR="#000000">[DEFENSE] :D:D:D</FONT></td></tr>
<tr><td>[08/08/2006 04:45:28]</td><td><FONT color="#FF0000">[P.A.K]DarkPrince</FONT></td><td><FONT COLOR="#000000">[DEFENSE] camper:(</FONT></td></tr>[/code]

It doesn't have to be caps sensitive, and the return page would actually be in html so I would see the tags, but just the wording.

Can anyone help me even start to understand to how to begin this script.

Share this post


Link to post
Share on other sites
I would use Perl for this. Are you restricted to PHP?

Share this post


Link to post
Share on other sites
Nope, I just figured php since it was something I had seen RSS/XML parsers before.

Any suggestions on how to do it in Perl?

Share this post


Link to post
Share on other sites
It doesn't look like you're parsing anything, but merely searching for lines that contain values.

Call this as [tt]perl name_of_log_file.log[/tt].

[code]
#!/usr/bin/perl
use warnings;
use strict;
while (<>) {
print if /Dark/;
}
[/code]

If you get that working, the hard coding can be removed.

Actually, if this doesn't get much more complex, use Unix's [b]grep[/b] command: [tt]grep Dark name_of_log_file.log[/tt].

Share this post


Link to post
Share on other sites
[code=php:0]
$search = "dark";
$data = //(do Fread stuff here to get your log file);
$pieces = explode("\n",$data);
$count = count($pieces);
for ($i=0;$i<=$count;$i++){
if (strpos($pieces[$i], $search){
echo "$pieces[$i]<br>/n";
}
}[/code]
[hr]
I haven't tested this, as I'm just writing it in QuickReply, but something along those lines should work.

Share this post


Link to post
Share on other sites
Fread stuff? I am not sure what you are referring to, however, the rest of your code was what I was examing earlier, or at least something similar.

Now my other question is how the heck do I get this to execute on my webpage, lol, like I said I am newbish to all this, I usually deal with editing prexisting php code and such.

Share this post


Link to post
Share on other sites
Use [tt]file[/tt] to read the lines into an array; don't worry about [tt]fread[/tt].

Share this post


Link to post
Share on other sites
[code]$lines_of_file = file('log_file');
foreach ($lines_of_file as $line) {
if (strstr($line, 'Dark')) {
  echo $line;
}
}[/code]

Share this post


Link to post
Share on other sites
#!/usr/bin/perl




$search = "dark";
$lines_of_file = file('http://63.210.148.20/63.210.148.20/CL-Current.html');
foreach ($lines_of_file as $line) {
if (strstr($line, $search)) {
  echo $line;
}
}


However, I get an internal server error, any thoughts?

Share this post


Link to post
Share on other sites
I posted PHP not Perl, since you didn't respond about the Perl. Don't use the [tt]#!/usr/bin/perl[/tt] line. Why do you use the IP address twice?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.