Jump to content

get part of file name


jacko310592

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

Archived

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

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