字符串不使用爆炸分裂

时间:2015-07-08 22:31:25

标签: php

我想在所有<br>上分割下面的字符串,但由于某种原因,我的循环返回一个巨大的字符串。我发誓我错过了一些简单但我无法看到的东西。谢谢!

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";
    }

文件内容:

<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

应该返回:

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 个答案:

答案 0 :(得分:1)

readfile()不会返回文件,因为您可以阅读手册:

  

返回从文件中读取的字节数。如果发生错误,则返回FALSE,除非函数被调用为@readfile(),否则将打印一条错误消息。

我认为你想要做的是:

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";
}