douzhang3822 2016-11-28 15:26
浏览 35
已采纳

echo -e使用foreach获取的文件内容

Forgive me, I'm not a programmer.

I am trying to include the last lines of a log file on a PHP page with two line breaks after each line read. Using responses from other questions, I came up with the following:

$file = "error.log";

foreach(file($file) as $line) {
        echo -e $line. "

";
}

When I execute this on the command line using php file.php, it is displayed correctly with the line breaks, however, on a webpage, it ignores the line breaks and prints a dirty output.

I tried to use echo -e in place of echo, but that doesn't even present the dirty output, but instead throws an error:

PHP Parse error: syntax error, unexpected '$line' (T_VARIABLE), expecting ',' or ';' in /var/www/err.php on line 12

I'm assuming echo -e isn't allowed within the foreach function? What am I doing wrong and how can I achieve the desired results?

  • 写回答

1条回答 默认 最新

  • dousi2013 2016-11-28 15:57
    关注

    echo in PHP is different than the echo command line in linux. You can't pass arguments to echo in PHP like that.

    try :

    echo nl2br($line . PHP_EOL);
    

    Or if you only care about the browser

    echo $line . "<br>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器