新线似乎不起作用。 php

New line doesn't seem to work. php

我 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 没有换行,如何解决这个问题?

如果您在 HTML 上下文中(在浏览器中)显示它,这样做是正常的,因为 HTML 会折叠成白色 space,包括换行符。将输出包装在 <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>';