I've test.php which contains:
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 contains
1
2
3
4
5
6
When i'm using it, php prints text like that 6 5 4 3 2 without new line, how to fix this?
If you're displaying it in an HTML context (in a browser) it's normal for it to do that because HTML collapses white space, including line breaks. Wrap your output in <pre>
tags to retain the line breaks.
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>';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.