php delete a line from log file that matches with some string value -
i new php. trying delete line log file matches string value.
below code able display matched line log file, wanna delete line , append rest of lines of log file.
below code:
<?php $searchthis = "mystring"; $matches = array(); $handle = @fopen("myfile.log", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); if(strpos($buffer, $searchthis) !== false) $matches[] = $buffer; } fclose($handle); } //show results: print_r($matches); ?> edit: lines in log file:
john abraham hml3 john abraham hml1 $search_string = 'hml3'
function
function search_and_delete($_file,$search_string){ $file = file($_file); foreach ($file $line_index => $line) { if(strpos($line, $search_string) !== false){ unset($file[$line_index]); break; } } file_put_contents($_file, implode("",$file)); } sample usage
search_and_delete("/var/a.txt","removing_line_conteins");
Comments
Post a Comment