How do you retrieve the first n lines of a file? (php) -


$filename = file ("gstbook.txt"); $rows = count ($filename);  // $char string added 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 tutorial on website. it's guestbook program i'm trying turn tiny chat use 1 of other apps.

where i'm stuck has these 2 lines:

  $oldrows = join ('', file ('gstbook.txt') ); 

and:

  fputs ($filename, $oldrows . chr(13) . chr(10) . $newrow); 

i can tell it's inputting entire file single string. outputting string. haven't been able figure out google how make input first n lines of file $oldrows variable.

the news

</tr>  

of each file line can act delimiter. bad news don't know go there.

would tell me how limit input of $oldrows variable n lines $filename or on how trim down $oldrows variable n lines?

correction:

i need last n lines of file (newest added end)

try

array_map( 'trim', file( $filename ) ); 

this return array of string, can use for/foreach


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -