Monday, January 25, 2010

Simple file/log search and dump

I often find the need to comb log files for IP addresses or certain terms... This will search a file for a term and then write the entire line(s) in which the term is found out to a new resultant file...

<?php
$file = ''; //FILE TO SEARCH
$find = ''; //TERM TO FIND
$outFn = $find.'.log.txt'; //OUTPUT FILENAME
$out_fp = fopen($outFn,'w' );

while($line = fgets( $fp ,1024 ))
{
 if( strpos( $line,$find ) !== FALSE ) 
 {
  fwrite( $out_fp , $line."\n" );
 }
}
?>