Jump to content

Recommended Posts

Hi all,

 

I was wondering if i can filter certain log files like using 2 keywords and get all the info inbetween? The log files are like <1 MB.

 

The other problem i encounter is howto since i looked all over the net but cant seem to find usefull info about it.

 

Can you help me out here?

Link to comment
https://forums.phpfreaks.com/topic/221741-filtering-files/
Share on other sites

There are probably several ways of doing this, but regular expression is awesome if you know what to expect.

 

file_get_contents(), to get the contents of a file.

 

preg_match(), if you want just one hit.

preg_match_all(), if you want more than one hit.

read the documentation for those two functions, you will see it can return an array of matches.

 

the regex should be like:

'/start.*?end/'

it will look for a start and then the first possible end.

Link to comment
https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147627
Share on other sites

Thank you MMDE for your fast answer.

 

That brought me to the php.net site where i found this.

 

<?php
// Found on php.net
// Returns an array of strings where the start and end are found
function findinside($start, $end, $text) {
preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)'. preg_quote($end, '/').'/i', $text, $m);
return $m[1];
}

$text = file_get_contents ('log.log'); 
$start = "from_text_a";
$end = "to_text_b";

$out = findinside($start, $end, $text);

print_r ($out);

?>

 

This just gives me an empty array.

Link to comment
https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147641
Share on other sites

preg_match_all();

Array of all matches in multi-dimensional array ordered according to flags.

preg_match_all($regex,$string,$matches);
$allmatches=array();
foreach($matches AS $match){
foreach($matches AS $thematch){
	$dupe=0;
	foreach($allmatches AS $uniquematches){
		if($thematch==$uniquematches){
			$dupe=1;
			break;
		}
	}
	if($dupe==0){
		$allmatches[]=$thematch;
	}
}
}
print_r($allmatches);

^ will filter dupe matches and put it all into the $allmatches array.

I always do a typo or two, so sorry if I've done it again.

Link to comment
https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147652
Share on other sites

Actually, I noticed I had to run another foreach, and I don't think you want to filter out dupes, but if you do, look at the one above...

 

preg_match_all($regex,$string,$matches);
$allmatches=array();
foreach($matches AS $match){
foreach($matches AS $thematch){
	foreach($thematch AS $matchrow){
		$allmatches[]=$matchrow;
	}
}
}
print_r($allmatches);

 

and since you're using some kind of log files, you might want to match something for more than one line...

$regex='/start[\s\S]*?end/';

where "start" is the first part of what you're looking for and "end" is the end of what you are looking for.

Link to comment
https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147667
Share on other sites

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.