views:

170

answers:

5

hi i want to delete all lines from old log files and keep fresh 50 lines which is at the bottom

plz give me the php code do something like this and if possible can we change the orientation of these lines

normal input

111111
2222222
3333333
44444444
5555555

output like

555555
4444444
3333333
22222
111111

to see the fresh log at top and 50 or 100 lines only

Thanks alot


Added Comment from below

Thanks alot but how to join it with this one ?

// set source file name and path 
$source = "toi200686.txt"; 
// read raw text as array 
$raw = file($source) or die("Cannot read file"); 
// join remaining data into string 
$data = join('', $raw); 
// replace special characters with HTML entities 
// replace line breaks with <br />  
$html = nl2br(htmlspecialchars($data)); 

it make the output as a html file, so how ur code will run with this properly ? Thanks

+1  A: 
$lines = file('/path/to/file.log'); // reads the file into an array by line
$flipped = array_reverse($lines); // reverse the order of the array
$keep = array_slice($flipped,0, 50); // keep the first 50 elements of the array

from there you can dow whatever with $keep. for example if you want to spit it back out:

echo implode("\n", $keep);

or

file_put_contents('/path/to/file.log', implode("\n", $keep));
prodigitalson
+1 though he might run into memory issues when the logfiles are large
Gordon
Thanks alot but how to join it with this one ? , // set source file name and path $source = "toi200686.txt"; // read raw text as array $raw = file($source) or die("Cannot read file"); // join remaining data into string $data = join('', $raw); // replace special characters with HTML entities // replace line breaks with <br /> $html = nl2br(htmlspecialchars($data));it make the output as a html file, so how ur code will run with this properly ?Thanks
MIkeyy
I would think it would be better to get the array slice before flipping it, to avoid duplicating what could be a large array. `$keep = array_reverse(array_slice($lines,count($lines)-$n-1, count($lines)-1))`.
Jeff B
yup that's right JeffB , but where to write 50 ? in that line ?and i got that working ( prodigitalson )
MIkeyy
The 50 goes in $n. So... `$keep = array_reverse(array_slice($lines,count($lines)-51, count($lines)-1))`
Jeff B
A: 

This would work for truncating the log file:

exec("tail -n 50 /path/to/log.txt", $log);
file_put_contents('/path/to/log.txt', implode(PHP_EOL, $log));

This will return the output from tail in $log and write it back to the log file.

Gordon
A: 

Optimal form is:

<?
print `tail -50 /path/to/file.log`;
?>
UncleMiF
I suggest you're on *NIX platform, my solution wont work on Windowzzz...
UncleMiF
sure does with Cygwin Tools installed. The problem with this approach is getting the newlines when writing back to the log
Gordon
+2  A: 

This is a bit more complex, but uses less memory as the entire file is not loaded into the array. Basically, it keeps an N length array, and pushes on new lines as they are read from the file while shifting one off. Because the newline is returned by fgets, you can simply do an implode to see your N lines, even with the padded array.

<?php
$handle = @fopen("/path/to/log.txt", "r");
$lines = array_fill(0, $n-1, '');

if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle);
        array_push($lines, $buffer);
        array_shift($lines);
    }
    fclose($handle);
}

print implode("",$lines);
?>

Just showing another way to do things, especially if you don't have tail at your disposal.

Jeff B
+1 for this as well. Definitely less resource intensive... i just happen to avoid resource handles if at all possible :-)
prodigitalson
i m confused now, tell me which one shud i use
MIkeyy
@Mikeyy: Depends on whether memory is a concern. Most likely it is not, considering these are text files, so either solution will work.
Jeff B
yea, so which is best, well i guess your's is the best one , i got all of these working :D
MIkeyy
may you just help me with this one, i need reuglar expressions, preg_replacewant to remove the whole line depends on word2----[word1] something-line-text [word2] some text again --into -->want to replace some text with another depends on word2--and[word1] something-line-text [word2] some text again--into-->REPLCACEDTEXT something-line-text some text again
MIkeyy
these are in brackets
MIkeyy
@Mikeyy: You lost me. I need a more specific description and/or an example. This might be better asked as a new question.
Jeff B
A: 

this method use associative array to store only $tail number of lines each time. It does not fill the whole array with all lines

$tail=50;
$handle = fopen("file", "r");
if ($handle) {
    $i=0;
    while (!feof($handle)) {
        $buffer = fgets($handle,2048);
        $i++;
        $array[$i % $tail]=$buffer;
    }
    fclose($handle);
    for($o=$i+1;$o<$i+$tail;$o++){
        print $array[$o%$tail];
    }
}
ghostdog74