PHP:仅显示前5行变量

时间:2012-03-10 17:30:38

标签: php

我有一个脚本来收发邮件,但我只想输出第一行(最近的)5行。每封电子邮件显示为

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

所以它不仅仅是以任何形式结束,而是它仍然做同样的事情。我猜最简单的方法是只允许5秒然后削减输出,但我怎么能这样做?

这是PHP,使用此脚本http://davidwalsh.name/gmail-php-imap

3 个答案:

答案 0 :(得分:2)

更新

<强>解决方法1:

在这段代码中:http://davidwalsh.name/gmail-php-imap,添加第二行代码:

 /* 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.

<强>溶液2:

在foreach循环部分中,编辑它:

/* 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>';
}

原始解决方案

另一种方式:

$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;

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

如果您在评论中说出决定是否有新行的DIV标签,您可以使用这样的正则表达式来执行此操作:

$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"; }