Jump to content

Filtering files


Sjaak

Recommended Posts

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

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.