Jump to content

Recommended Posts

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);

 

 

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));
?>

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 (:

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;
?>

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

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;

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.