PHP foreach()仅返回最后50个项目

I am currently using the following PHP code to return and format output from an .htm file:

<?php
$es = "</span>";
$lines = file("http://www.example.com/log.htm", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($lines as $line){
list($date, $nick, $message) = explode(" ", $line, 3);
$str = substr($nick, 4, 1);
if($str=="+"){
  $nickspan = '<span style="color:teal">';
  $nick = substr_replace($nick, "", 2, 2);
  echo("$date $nickspan$nick$es $message <br/>
  ");
} else {
  if($str=="@"){
    $nickspan = '<span style="color:#F20707">';
    $nick = substr_replace($nick, "", 2, 2);
    echo("$date $nickspan$nick$es $message <br/>
    ");
} else {
    $str = substr($nick, 3, 1);
    if($str=="*"){
    $nickspan = '<span style="color:purple">';
    $nick = substr_replace($nick, "", 1, 2);
     echo("$date $nickspan$nick $message$es <br/>
     ");
    } else {
      $nickspan = '<span style="color:grey">';
      $str = substr($nick, 2, 2);
      if($str=="03"){
        $nick = substr_replace($nick, "", 2, 2);
        }
      echo("$date $nickspan$nick$es $message <br/>
      ");
    } } } }
?>

How would I change this to only display the last 50 lines, instead of listing absolutely everything from what is essentially a log file?

You're looking for the array_slice function:

array_slice() returns the sequence of elements from the array array as specified by the offset...

$lines = file(...);
$lines = array_slice($lines, -50);

Use array_slice() to slice off the last 50 elements of $lines prior to running the foreach() { } loop.

$lines = array_slice($lines, -50);

Another alternative is to do the calculation your own and skip all but the X last ones:

$last  = 50;
$count = count($lines);    
foreach ($lines as $line) 
{
    if ($count-- > $last) continue;
    ...