Load File, Remove last few lines





2
Date Submitted Fri. Aug. 25th, 2006 3:44 AM
Revision 1 of 1
Helper Cyber-Drugs
Tags ??? | function | implode | phpcode | return | unset | while
Comments 0 comments
Below is a bit of code, which I needed to make when creating an application which did a lot of file manipulation. If you ever want to load the contents of a file, but remove X amount of lines from the end of the file, use the function below.

<?php
//
// Load a text file, but remove the last X amount of lines
//
function end_file($filename, $lines);
{
    $contents = file($filename);
    $i = count($contents);
    $c = $i-$lines;
    while ($c <= $i)
    {
        unset($contents[$c]);
        $c++;
    }
    $contents = implode("",$contents);
    return($contents);
}
?>
 

Justin Nel

Comments

There are currently no comments for this snippet.

Voting