使用php从文本文件中删除行

时间:2017-04-15 15:47:59

标签: php

我有一个文本文件如下

href='https://www.example.com/tv/xxxxxxxx/playlist.m3u8'>TV 1
href='https://www.example.com/tv/xxxxxxxx/playlist.m3u8'>TV 2
href='https://www.example.com/tv/xxxxxxxx/playlist.m3u8'>TV 3
href='https://www.example.com/tv/xxxxxxxx/playlist.m3u8'>TV 4
href='https://www.example.com/tv/xxxxxxxx/playlist.m3u8'>TV 5
href='https://www.example.com/playlist/xxxxxxxx/playvod/vybz9h.mp4'>Film 1
href='https://www.example.com/playlist/xxxxxxxx/playvod/tq5mzt.mp4'>Film 2
href='https://www.example.com/playlist/xxxxxxxx/playvod/xegtnw.mp4'>Film 3
href='https://www.example.com/playlist/xxxxxxxx/playvod/16c9os.mp4'>Film 4
href='https://www.example.com/playlist/xxxxxxxx/playvod/r25dwc.mp4'>Film 5

作为一个非常长的文本文件,我想消除M3U8连接的唯一行,并仅保留那些带有电影链接的行。 可以这样做吗?

1 个答案:

答案 0 :(得分:0)

$lines = file("filename");

$result = "";

foreach($lines as $line){                                                                                                                                                                 

    if(strpos($line, "M3U8")){                                                                                                                                                                   
        $line = "";                                                                                                                                                                               
    }                                                                                                                                                                                         

    $result .= $line;                                                                                                                                                                         
}

file_put_contents("filename", $result);

希望这会有所帮助。 :)