Jump to content


Photo

Help parsing HTML log file


  • Please log in to reply
11 replies to this topic

#1 mrmagoo_83

mrmagoo_83
  • Members
  • PipPip
  • Member
  • 10 posts
  • LocationFort Worth. Tx

Posted 09 August 2006 - 03:36 PM

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.
<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>

I want the script to run such that if I search for "Dark" I would get the return of:
<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>

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.



#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 09 August 2006 - 03:49 PM

I would use Perl for this. Are you restricted to PHP?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 mrmagoo_83

mrmagoo_83
  • Members
  • PipPip
  • Member
  • 10 posts
  • LocationFort Worth. Tx

Posted 09 August 2006 - 03:53 PM

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?

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 09 August 2006 - 04:15 PM

It doesn't look like you're parsing anything, but merely searching for lines that contain values.

Call this as perl name_of_log_file.log.

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

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

Actually, if this doesn't get much more complex, use Unix's grep command: grep Dark name_of_log_file.log.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 09 August 2006 - 04:21 PM

$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";
}
}
 
I haven't tested this, as I'm just writing it in QuickReply, but something along those lines should work.

#6 mrmagoo_83

mrmagoo_83
  • Members
  • PipPip
  • Member
  • 10 posts
  • LocationFort Worth. Tx

Posted 09 August 2006 - 06:21 PM

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.

#7 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 09 August 2006 - 06:23 PM

Use file to read the lines into an array; don't worry about fread.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#8 mrmagoo_83

mrmagoo_83
  • Members
  • PipPip
  • Member
  • 10 posts
  • LocationFort Worth. Tx

Posted 09 August 2006 - 06:53 PM

Still lost, sorry :(

#9 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 09 August 2006 - 07:22 PM

$lines_of_file = file('log_file');
foreach ($lines_of_file as $line) {
 if (strstr($line, 'Dark')) {
   echo $line;
 }
}

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#10 mrmagoo_83

mrmagoo_83
  • Members
  • PipPip
  • Member
  • 10 posts
  • LocationFort Worth. Tx

Posted 09 August 2006 - 08:13 PM

#!/usr/bin/perl




$search = "dark";
$lines_of_file = file('http://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?


#11 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 09 August 2006 - 08:33 PM

I posted PHP not Perl, since you didn't respond about the Perl. Don't use the #!/usr/bin/perl line. Why do you use the IP address twice?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#12 mrmagoo_83

mrmagoo_83
  • Members
  • PipPip
  • Member
  • 10 posts
  • LocationFort Worth. Tx

Posted 10 August 2006 - 01:48 PM

Thanks so much. I got it to work great.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users