...

View Full Version : Quick RegExp Question



DaveHope
10-04-2003, 08:40 PM
Hello all,

I'm currently getting information from my apache logs to show information on hits and other information. using the following:

strstr($strLogFile[$i],"Oct") ? $strOctHits++ : $strOctHits = $strOctHits;

That works really well, however now for some other information I'll need to see if two values are present in $strLogFile[$i] I've looked through the manual on RegExp and found little useful information and since I've never used RegExp before I was wondering if someone had a good example of looking for two bits of data on one line? thanks!

missing-score
10-04-2003, 08:56 PM
if(preg_match("/.(value1).(value2)./is")){

}

should work i think, but it really depends what you are doing.... could you be any more specific?

DaveHope
10-04-2003, 09:18 PM
More Specific? - Sure, no problem.

Lets say $strLogFile[0] is :

195.92.67.66 - - [03/Oct/2003:07:41:53 +0100] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (compatible; Konqueror/3.1; Linux)"

I;d want to count every " 404 " in the month of October. The code I mentioned in my first post goes through counting up the hits per month (counting up the ammount of times "Oct" comes up. However, now I need to count the ammount of times " 404 " comes up when the month is October ;P

missing-score
10-04-2003, 09:33 PM
$strOct404 = preg_match("/.*Oct.*404.*/is", $strLogFile[$i]) ? $strOct404++ : $strOct404;


you could try that

DaveHope
10-04-2003, 09:40 PM
Thanks, but alas thats returning a 0.

$strOct404 = preg_match("/.*Oct.*404.*/is", $strLogFile[$i]) ? $strOct404++ : $strOct404; if ($strOct404 == "") { $strOct404 = "0"; }

mordred
10-04-2003, 10:15 PM
You have been tricked by the way how post-increment ($var++) works. The value is incremented after the value has been returned, so you always assign 0 to $strOct404 on each iteration step. Using pre-increment instead increments first and then returns the value. Try if this works for you:

$strOct404 = preg_match("/.*Oct.*404.*/is", $strLogFile[$i]) ? ++$strOct404 : $strOct404;

BTW: Why is the variable called 'strOct404'? I suppose that you use hungarian notation, but then... the variable is an integer? Eh?

DaveHope
10-04-2003, 10:26 PM
Thanks to the both of you!!!:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum