dourong8495 2015-07-08 22:31
浏览 20
已采纳

字符串不使用爆炸分裂

I am trying to split the string below on all <br> but for some reason my loop returns one giant string. I swear Im missing something simple but I cant see it. Thanks!

if(file_exists("/home/pi/Desktop/PiControl/status_log"))
    {
        $file = readfile("/home/pi/Desktop/PiControl/status_log");
        $arr= explode("<br>",$file);
        //$file=array_slice($file,count($file)-5);
        //$file=array_reverse($file);
        foreach($arr as $f)
        {
            echo $f;
            echo "WOO";
        }   
        fclose($file);
    }
    else
    {
        echo "no file found";
    }

contents of the file:

<br>light is now: on 2015-06-01 03:32:42.902387<br>light is now: off 2015-06-01 03:32:54.360173<br>light is now: on 2015-06-01 03:33:07.781693<br>light is now: off 2015-06-01 03:33:15.867386<br>light is now: on 2015-06-01 03:34:42.683736<br>light is now: off 2015-06-01 03:34:45.205557<br>light is now: on 2015-06-01 03:36:18.037309<br>light is now: off 2015-06-01 03:36:25.915424<br>light is now: on 2015-06-01 03:36:58.261714<br>light is now: off 2015-06-01 03:37:05.103494<br>light is now: on 2015-06-01 11:00:01.735592<br>light is now: off 2015-06-01 13:06:50.254778<br>light is now: on 2015-06-01 17:28:11.218417

should return:

light is now: on 2015-06-01 03:32:42.902387
WOO
light is now: off 2015-06-01 03:32:54.360173
WOO
.....
  • 写回答

1条回答 默认 最新

  • donglu3184 2015-07-08 22:35
    关注

    readfile() doesn't return the file as you can read in the manual:

    Returns the number of bytes read from the file. If an error occurs, FALSE is returned and unless the function was called as @readfile(), an error message is printed.

    I think what you want to do is this:

    if(file_exists("/home/pi/Desktop/PiControl/status_log")) {
    
        $lines = explode("<br>", file_get_contents("/home/pi/Desktop/PiControl/status_log"));
    
        foreach($lines as $line) {
            echo $line . "<br>";
            echo "WOO<br>";
        }
    
    
    } else {
        echo "no file found";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助