2017-04-09 46 views
0

大家早上好,從互聯網上下載一個文本文件,其中包含許多指向電視節目和電影流媒體頻道的鏈接,但它是以隨機順序呈現的,有一種方法PHP根據我可以設置我可能通過一個數組或什麼的順序重新排序每一行? 這是一個示例,說明如何使用頻道電視名稱作爲唯一來源在所有行中更改的複合視頻和文件,以提供不同的順序。他們是初學者,是我在網絡中尋找如何做的日子,但是我找不到可以成爲我的案例的例子。謝謝。放入txt文件的自然順序行

<a href='http://example.com:80/tv/example/playlist.m3u8'>AdamTV</a> 
<a href='http://example.com:80/tv/example/playlist.m3u8'>Skynews</a> 
<a href='http://example.com:80/tv/example/playlist.m3u8'>NaturalTV</a> 
<a href='http://example.com:80/tv/example/playlist.m3u8'>SportTV</a> 
<a href='http://example.com:80/tv/example/playlist.m3u8'>Channel4</a> 

這就是我想要做的,開始讀一行文件中的行,但我不推薦使用錯誤:功能分割()已過時

<?php 
$linee = file("file.txt"); 
while(list(,$value) = each($linee)) { 
list($url, $channel) = split("[>]", $value); 
$params["url"] = trim($url); 
$params["channel"] = trim($channel); 
echo $params["url"]." ".$params["channel"]; 
} 

回答

0

讀取文件到數組,然後排序與usort陣列 - 這將允許您從鏈接(例如,與preg_match)提取電視節目名稱並比較它們。

例子:

$linee = [ 
    "<a href='http://example.com:80/tv/example/playlist.m3u8'>AdamTV</a>", 
    "<a href='http://example.com:80/tv/example/playlist.m3u8'>Skynews</a>", 
    "<a href='http://example.com:80/tv/example/playlist.m3u8'>NaturalTV</a>", 
    "<a href='http://example.com:80/tv/example/playlist.m3u8'>SportTV</a>", 
    "<a href='http://example.com:80/tv/example/playlist.m3u8'>Channel4</a>", 
]; 

usort(
    $linee, 
    function($a, $b) { 
     preg_match("/>(.*)</", $a, $a_val); 
     preg_match("/>(.*)</", $b, $b_val); 

     if ($a_val[0] == $b_val[0]) { 
      return 0; 
     } 
     return ($a_val[0] < $b_val[0]) ? -1 : 1; 
    } 
); 

print_r($linee); 
+0

老實說,我真的沒有經驗,你可以採取的我該怎麼辦,grazie.Sono天,我嘗試和我嘗試劇本適合我的需要的例子,但無效 – Antony

+1

請編輯您的文章與你的嘗試代碼。否則我懷疑你會在這裏得到任何幫助。 – ghostprgmr

+0

我修改了原來的帖子,建議將工作完成到現在,謝謝 – Antony