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 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解