$fileName = file ("gstbook.txt");
$rows = count ($fileName);
// $Char is the string that will be added to the file.
if ($Char != '')
{
$Char = str_replace ("\n","<br>",$Char);
$Char = strip_tags ($Char, '<br>');
$newRow = '<tr><td>' . ($username) . ": " . ($Char) . '</td></tr>';
$oldRows = join ('', file ('gstbook.txt') );
$fileName = fopen ('gstbook.txt', 'w');
fputs ($fileName, $oldRows . chr(13) . chr(10) . $newRow);
fclose ($fileName);
}
i'm working from a tutorial on a website. it's a guestbook program that i'm trying to turn into a tiny chat for use with one of my other apps.
where i'm stuck has to do with these two lines:
$oldRows = join ('', file ('gstbook.txt') );
and:
fputs ($fileName, $oldRows . chr(13) . chr(10) . $newRow);
I can tell it's inputting the entire file into a single string. then outputting that string. what i haven't been able to figure out from Google is how to make it input only the first n lines of a file into the $oldRows variable.
the good news is that the
</tr>
of each file line can act as a delimiter. The bad news is i don't know where to go from there.
Would anyone tell me how to limit the input of the $oldRows variable to just n lines from $filename OR on how to trim down the $oldRows variable to just n lines?
CORRECTION:
i need to get the LAST n lines of the file (newest is added to the end)
Try
array_map( 'trim', file( $filename ) );
this will return array of string, then you can use for/foreach