2013-05-31 2 views
0

영화 제목과 등급을 반환하는 PHP 작업 스크립트를 가지고 있습니다.PHP 텍스트 파일에서 읽은 오름차순 데이터로 정렬

는 PHP 스크립트는 다음과 같습니다

<?php 
header("content-type: text/plain"); 

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 "Movie List for $key 2013\n\n"; 
    } 
} 

$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 "{$info["title"]} : {$info["rating"]}\n"; 
    } 
} 

출력은 다음과 같습니다

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와 영화 있도록 표시 할 수있는 방법 바닥에? ANY 도움을 주시면 감사하겠습니다.

답변

1

가장 간단한 방법은 아마도 usort을 사용하는 것입니다. $titles의 항목을 비교하는 함수를 정의합니다.

관련 문제