2013-06-03 41 views
0

我有一個工作的PHP腳本,通過一個巨大的文件,並拉出特定的電影標題和評級。但現在我的任務是對它們進行「排序」,所以帶有'xxx'的電影名稱列在最下面。我已經研究過使用,但是用我已經寫過的腳本,這是否是最好的方法?還是有更簡單的方法?在我的php腳本的這種情況下,最好使用usort嗎?

php腳本:

<?php 

foreach (glob("*.mov") as $filename) 

$theData = file_get_contents($filename) or die("Unable to retrieve file data"); 

$months = ['January' => '_01', 'February' => '_02', 'March' => '_03', 'April' => '_04', 'May' => '_05', 'June' => '_06', 'July' => '_07', 'August' => '_08', 'September' => '_09', 'October' => '_10', 'November' => '_11', 'December' => '_12']; 

foreach($months as $key => $month){ 
    if(strpos($filename,$month)!==false){ 
     echo "<div style ='text-shadow: 0 1px 0 #222222; margin-left: 5%; margin-top: 20px; margin-bottom: 10px; font:18px Verdana,tahoma,sans-serif; 
       color:#218555; font-weight:bold;'>- Movie List for $key 2013 -</div>"; 

    } 
} 


$string = $theData; 
$titles = explode("\n", $string); 

function getInfo($string){ 
    $Ratings = ['G', 'PG', 'PG-13', 'R', 'NR', 'XXX']; 
    $split = preg_split("/\"(.+)\"/", $string, -1, PREG_SPLIT_DELIM_CAPTURE); 
    if(count($split) == 3){ 
     preg_match("/(".implode("|", $Ratings).")\s/", $split[0], $matches); 
     $rating = $matches[0]; 
     return ["title" => $split[1], "rating" => $rating]; 
    } 
    return false; 
} 

foreach($titles as $title){ 
    $info = getInfo($title); 
    if($info !== false){ 
     echo "<div style ='margin-left:5%; margin-bottom: 3px; 
       font:14px Verdana,tahoma,sans-serif;color:green;'> 
       {$info["title"]} : {$info["rating"]}</div>"; 
    } 
} 
?> 

輸出:

- Movie List for May 2013 - 
(HD) Identity Thief : PG-13 
(HD) Escape from Planet Earth : PG 
(HD) Dark Skies : PG-13 
(HD) The Guilt Trip : PG-13 
(HD) Jack Reacher : PG-13 
(HD) Les Miserables : PG-13 
(HD) Mama : PG-13 
(HD) Safe Haven : PG-13 
(HD) A Place at the Table : PG 
(HD) Cirque du Soleil: Worlds Away : PG 
(HD) Rise of the Guardians : PG 
(HD) Fun Size : PG-13 
(HD) Shanghai Calling : PG-13 
(HD) The Package : NR 
(HD) The House at the End of the Street : PG-13 
Beautiful Creatures : PG-13 
The Incredible Burt Wonderstone : PG-13 
Jack the Giant Slayer : PG-13 
Parental Guidance : PG 
The Hobbit: An Unexpected Journey : PG-13 
Cloud Atlas : PG-13 
Life of Pi : PG 
Chasing Mavericks : PG 
Taken 2 : PG-13 
Adult title 1 : XXX 
Fat Burning Hip Hop Dance Party : G 
Fat Burning Hip Hop Dance Grooves : G 
Aladdin : G 
Americano : NR 
Missing Brendan : NR 
Point Doom : NR 
Gullivers Travels : G 
The Little Princess : PG 
Jack And The Beanstalk : PG 
To the Arctic : G 
Adult title 2 : XXX 

需要獲得技術職稱XXX排序的底部。

+0

嘗試使用asort。 –

回答

1

我建議你先建立的信息細節像這樣的列表:

$infolist = array(); 
foreach($titles as $title){ 
    $info = getInfo($title); 
    if($info !== false){ 
     $infolist[] = $info; 
    } 
} 

然後你就可以輕鬆地排序與usort列表由於評級是現在交通方便:

usort($infolist, "infosort"); 

function infosort($lhs,$rhs) { 
    return strcmp($lhs['rating'], $rhs['rating']); 
} 

最後你可以寫出來自infolist數組的排序結果:

foreach ($infolist as $info) { 
    echo "<div style ='margin-left:5%; margin-bottom: 3px; 
      font:14px Verdana,tahoma,sans-serif;color:green;'> 
      {$info["title"]} : {$info["rating"]}</div>"; 
} 
+0

它在通過函數之前給了我噸':'之後正確回聲。有沒有辦法不顯示':'的任何數據沒有得到解析。換句話說,我用冒號分隔45行,然後以有序的方式得到我真正想要的數據。 – ValleyDigital

+0

我可能應該在'getInfo'調用後包含一個false檢查。將更新答案以顯示我的意思。 –

+0

噢,我剛剛在infolist [] ... if($ info!== false)之前加了一句:{ 非常感謝你!James Holderness – ValleyDigital

相關問題