简体   繁体   中英

PHP : Only display First 5 Lines of Variable

I've got a script to get emails, but I only want to output the first (most recent) 5 lines. Each email is displayed as

<div class="toggler unread"><span class="subject">Email Subject</span> </div>

so it's not simply ending in or whatever, but it still does the same thing. I'm guessing the easiest way would be to only allow 5 s then cut the output, but how could I do this?

It's PHP, using this script, http://davidwalsh.name/gmail-php-imap

Update:

Solution1:

In this piece of code: http://davidwalsh.name/gmail-php-imap ,add the second line of code:

 /* put the newest emails on top */
 rsort($emails);

 // add this line    
 $emails = array_slice($emails,0,5); // <---- this will consider only the first 5 elements.

Solution2:

In the foreach loop section, edit it like this:

/* for every email... */
$i = 0;  // <---- a counter variable
foreach($emails as $email_number) {
  $i++;  // <---- increment the counter
  if($i > 5) break; // <---- after 5 mails are accessed, exit the loop

  /* get information specific to this email */
  $overview = imap_fetch_overview($inbox,$email_number,0);
  $message = imap_fetchbody($inbox,$email_number,2);

  /* output the email header information */
  $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
  $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
  $output.= '<span class="from">'.$overview[0]->from.'</span>';
  $output.= '<span class="date">on '.$overview[0]->date.'</span>';
  $output.= '</div>';

  /* output the email body */
  $output.= '<div class="body">'.$message.'</div>';
}

Original Solution

Another way:

$content = "Hello. 
            This is some 
            kick butt 
            content!
            Even
            more
            lines
            ...";

$splitContent = implode("\n",array_slice(explode("\n",$content),0,5));  //split the lines, then select only the first 5 elements and now join it back it as a string
echo $splitContent;
$content = "Hello. 
This is some 
kick butt 
content!
Even
more
lines
...";

$splitContent = split("\n", $content);
$line = 0;

while($line < 5) {
   echo $splitContent[$line];
   $line++;
}

If it's like you say in your comment the DIV-tags which decides if there's a new line, you could do it with a regular expression like this:

$alldata = '';
$alldata .= '<div class="toggler unread"><span class="subject">Email 1 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 2 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 3 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 4 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 5 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 6 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 7 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 8 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 9 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 10 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 11 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 12 Subject</span> </div>';
$alldata .= '<div class="toggler unread"><span class="subject">Email 13 Subject</span> </div>';
// 13 times
$allarr[] = $alldata;

preg_match_all('#(<div class="toggler\ unread">(.|\r|\n)*?</div>)#i', $alldata, &$lines);

for ($n = 0; $n<5; $n++) { echo $lines[0][$n] . "\n"; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM