新线似乎不起作用。 PHP

时间:2016-04-20 17:37:17

标签: php file newline

我的test.php包含:

function tailShell($filepath, $lines = 1) {
        ob_start();
        passthru('tail -'  . $lines . ' ' . escapeshellarg($filepath));
        return trim(ob_get_clean());
    }
    $test = tailShell('som.log',3);
    echo $test;

som.log包含

1
2
3
4
5
6

当我使用它时,php会打印 6 5 4 3 2 这样的文字而没有新行,如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果您在HTML上下文中(在浏览器中)显示它,那么这是正常的,因为HTML会折叠空白区域,包括换行符。将您的输出换行到<pre>标记以保留换行符。

function tailShell($filepath, $lines = 1) {
    ob_start();
    passthru('tail -'  . $lines . ' ' . escapeshellarg($filepath));
    return trim(ob_get_clean());
}
$test = tailShell('som.log',3);
echo '<pre>'. $test .'</pre>';
相关问题