Sjaak Posted December 15, 2010 Share Posted December 15, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/221741-filtering-files/ Share on other sites More sharing options...
MMDE Posted December 15, 2010 Share Posted December 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147627 Share on other sites More sharing options...
Sjaak Posted December 15, 2010 Author Share Posted December 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147641 Share on other sites More sharing options...
MMDE Posted December 15, 2010 Share Posted December 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147652 Share on other sites More sharing options...
MMDE Posted December 15, 2010 Share Posted December 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147667 Share on other sites More sharing options...
Sjaak Posted December 15, 2010 Author Share Posted December 15, 2010 Thank you, but now im very confused. What is the .log file to search in? Quote Link to comment https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147672 Share on other sites More sharing options...
MMDE Posted December 15, 2010 Share Posted December 15, 2010 Thank you, but now im very confused. What is the .log file to search in? $file='logfile.log'; $string=file_get_contents($file); Quote Link to comment https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147677 Share on other sites More sharing options...
Sjaak Posted December 15, 2010 Author Share Posted December 15, 2010 Thank you so much. Do you also know how i can add a break to every print line? Quote Link to comment https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147685 Share on other sites More sharing options...
MMDE Posted December 15, 2010 Share Posted December 15, 2010 print_r was just to show you the array you are left with... to echo it like you want it to: foreach($allmatches AS $allmatchesrow){ echo $allmatchesrow.'<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147699 Share on other sites More sharing options...
Sjaak Posted December 15, 2010 Author Share Posted December 15, 2010 Thank you again. I must go work, i will test later tonight. Again, many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/221741-filtering-files/#findComment-1147708 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.