PHP的foreach()只返回最后50个项目 [英] PHP foreach() return only last 50 items
本文介绍了PHP的foreach()只返回最后50个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下PHP代码从.htm文件返回并格式化输出:
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/>
");
} } } }
?>
我如何改变它只显示最后50行,而不是列出绝对的东西本质上是一个日志文件?
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:
code> array_slice()返回由 offset 指定的数组数组中元素的序列。
array_slice()
returns the sequence of elements from the array array as specified by the offset...
$lines = file(...);
$lines = array_slice($lines, -50);
这篇关于PHP的foreach()只返回最后50个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文