在PHP中返回文件的最后10行的问题


Issue with returning the last 10 lines of a file in PHP

目前,我有这个代码来返回文件中的最后10行:food.txt

<?php
echo `tail /home/food.txt`;

输出返回类似于:

pie apple orange fish lemon peach egg bacon pancake strawberry

问题是,在PHP中运行tail命令会忽略文件food.txt中的原始行,如果我在SSH上运行相同的命令,我会得到:

pie 
apple 
orange 
fish 
lemon 
peach 
egg 
bacon 
pancake 
strawberry

我无法使用PHP打开文件(据我所知),因为随着时间的推移,文件会变得很大,我不确定PHP是否能够处理它,总的来说,我的意思是250000行以上。

谢谢。

如果问题缺少换行符(问题不清楚),则将所有内容都封装在<pre>中。

echo '<pre>';
echo `tail /home/food.txt`;
echo '</pre>';

编辑添加:默认情况下,<pre>会给你一个单空间字体;如果出现问题,可以使用CSS轻松解决。

我们只能使用一个<pre>标签

第一个

echo '<pre>';
echo 'tail /home/food.txt';

第二个

$string = 'tail /home/food.txt';
echo str_replace(" " ,"'n", $string);