jacko310592 Posted January 17, 2010 Share Posted January 17, 2010 hey guys i have a glob function on my site to gather logFiles, on each log file the file name goes as follows logFile (month.year) [number of hits in that month].txt is there a function which will allow me to just grab the text within the [ ] thanks guys Quote Link to comment Share on other sites More sharing options...
rpmorrow Posted January 17, 2010 Share Posted January 17, 2010 $openPos = strpos($filename, "["); $closePos= strrpos($filename, "]", $openPos); $hits = substr($filename, $openPos, $closePos - $openPos) Quote Link to comment Share on other sites More sharing options...
salathe Posted January 17, 2010 Share Posted January 17, 2010 There are lots of ways of doing this: you could use a regular expression (preg_match), string functions (strpos, strstr, strtok, etc.), or you could even go crazy with sscanf like $filename = 'logFile (month.year) [number of hits in that month].txt'; sscanf($filename, '%*[^[][%[^]]', $hits); var_dump($hits); Quote Link to comment Share on other sites More sharing options...
crabfinger Posted January 17, 2010 Share Posted January 17, 2010 This is another easy way. <?php $string = 'logFile (month.year) [number of hits in that month].txt'; $pattern = '/\[(.+)\]/i'; if(preg_match($pattern,$string,$matches) == 1) { print $matches['1']; } else { print 'No matches'; } ?> Quote Link to comment Share on other sites More sharing options...
jacko310592 Posted January 17, 2010 Author Share Posted January 17, 2010 thanks for the replies guys, it helped a lot (: Quote Link to comment Share on other sites More sharing options...
crabfinger Posted January 17, 2010 Share Posted January 17, 2010 Hell you could even get fancy and make a function to do it nicely <?php function getLogFile($string) { $pattern = '/(.+?) \((.+?)\) \[(.+?)\]/i'; $array = !is_array($string) ? array($string) : $string; foreach($array as $key => $logFile) { if(preg_match($pattern,$logFile,$matches) == 1) { list($return[$key]['string'],$return[$key]['name'],$return[$key]['date'],$return[$key]['hits']) = $matches; } else { $return[$key]['string'] = $logFile; $return[$key]['error'] = 'No matches'; } } return $return; } $logFileA = 'logFile (month.year) [number of hits in that month].txt'; print_r(getLogFile($logFileA)); $logFileB[] = 'logFile (month.year) [number of hits in that month].txt'; $logFileB[] = 'logFile (month.year) [number of hits in that month].txt'; $logFileB[] = 'logFile (month.year) [number of hits in that month].txt'; print_r(getLogFile($logFileB)); ?> Quote Link to comment Share on other sites More sharing options...
jacko310592 Posted January 17, 2010 Author Share Posted January 17, 2010 i actually have another problem now guys, your code work great, but then i realised that i need to add the results of several files together, for example : logFile (01.10) [20].txt logFile (12.09) [15].txt logFile (11.09) [10].txt which using a code you guys provided, gives me either 20, 15 or 10, but i need to have these results added together (eg. 45), can someone please suggest a way, my mind is dead tonight. this is the code i have so far: $logFiles = glob("*.txt", GLOB_NOSORT); for ($i=0; $i<count($logFiles); $i++) $filename = $logFiles[$i]; sscanf($filename, '%*[^[][%[^]]', $hits); echo $hits; and thanks for your extended post crabfinger, ill have a closer look at that once i have this problem sorted (: Quote Link to comment Share on other sites More sharing options...
jacko310592 Posted January 17, 2010 Author Share Posted January 17, 2010 anyone have any ideas? thanks Quote Link to comment Share on other sites More sharing options...
crabfinger Posted January 17, 2010 Share Posted January 17, 2010 This is how you would do it using the function i made earlier. <?php function getLogFile($string) { $pattern = '/\[(.+)\]/i'; $array = !is_array($string) ? array($string) : $string; foreach($array as $key => $logFile) { if(preg_match($pattern,$logFile,$matches) == 1) { $return[$key] = $matches['1']; } else { $return[$key]['string'] = $logFile; $return[$key]['error'] = 'No matches'; } } return $return; } $logFiles = glob("*.txt", GLOB_NOSORT); $logHits = getLogFile($logFiles); $logHitsT = 0; foreach($logHits as $value) { $logHitsT = $logHitsT + $value; } print $logHitsT; ?> Quote Link to comment Share on other sites More sharing options...
jacko310592 Posted January 17, 2010 Author Share Posted January 17, 2010 hey, thanks for anyother post crabfinger, i tried your code, and i got this error message "Fatal error: Unsupported operand types in E:\xampp\htdocs\logs\index.php on line 143 ", which is refaring to this line: "$logHitsT = $logHitsT + $value;". and just before you posted, the code which i manged to do was just: $logFiles = glob("*.txt", GLOB_NOSORT); for ($i=0; $i<count($logFiles); $i++) { $logFile = $logFiles[$i]; sscanf($logFile, '%*[^[][%[^]]', $hits); echo' '.$hits.''; } which with logFiles such as logFile (01.10) [20].txt logFile (12.09) [15].txt logFile (11.09) [10].txt outputs: 20 15 10 but im not sure how to make it add these values together..? any ideas for either code? thanks guys Quote Link to comment Share on other sites More sharing options...
crabfinger Posted January 18, 2010 Share Posted January 18, 2010 $logFiles = glob("*.txt", GLOB_NOSORT); for ($i=0; $i<count($logFiles); $i++) { $logFile = $logFiles[$i]; sscanf($logFile, '%*[^[][%[^]]', $hits); $hits = $hits+value } print $hits; Quote Link to comment Share on other sites More sharing options...
jacko310592 Posted January 18, 2010 Author Share Posted January 18, 2010 thanks again crabfinger, but that code seems to just output a value of "1" no matter what. =/ any ideas guys? Quote Link to comment Share on other sites More sharing options...
jacko310592 Posted January 18, 2010 Author Share Posted January 18, 2010 ---correction, it only outputs the value of the last file found, rarther than all the values added together Quote Link to comment Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 Its because the code for $hits is over-writing itself $total_hits = 0; $logFiles = glob("*.txt", GLOB_NOSORT); for ($i=0; $i<count($logFiles); $i++) { $logFile = $logFiles[$i]; sscanf($logFile, '%*[^[][%[^]]', $hits); $total_hits += $hits; } print $total_hits; Quote Link to comment Share on other sites More sharing options...
jacko310592 Posted January 18, 2010 Author Share Posted January 18, 2010 hi Buddski, your code seems to random vaues close to the correct value, but i arent sure whats going wrong. for example [2] //gives 4 [2] + [3] + [1] + [2] //gives 10 [2] + [3] + [1] + [2] + [4] + [5] + [10] + [1] //gives 29 Quote Link to comment Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 I dont know what your saying.. I just tested it and got this.. log[10].txt= Hits: 10 log[15].txt= Hits: 15 log[25].txt= Hits: 25 Total: 50 Quote Link to comment Share on other sites More sharing options...
jacko310592 Posted January 18, 2010 Author Share Posted January 18, 2010 hmm, i got a total of 75 using the same example you did below Quote Link to comment Share on other sites More sharing options...
jacko310592 Posted January 18, 2010 Author Share Posted January 18, 2010 ahh, problem solved, for some reason, it was also adding files within the same directory which didnt have the .txt ext, so i changed this line "$logFiles = glob("*.txt", GLOB_NOSORT);" to "$logFiles = glob("logFile *.txt", GLOB_NOSORT);" thanks for your help Buddski Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.