0

我创建了一个 php 文件,我试图用它来解析数据。我要解析的文件内容如下所示:

[Titles]
  hollywoodhd1 1 0 8046 0 919 PG-13 6712 1 identity_hd "(HD) Identity Thief" Disk 0 04/15/13 11/01/13 0 0 0 0 0 0 1 1 0 16000000 H3 16:9 0 0
  hollywoodhd2 3 0 8016 0 930 PG 5347 1 escapep_hd "(HD) Escape from Planet Earth" Disk 0 04/01/13 10/01/13 0 0 0 0 0 0 1 1 0 16000000 H3 16:9 0 0
  hollywoodhd3 1 0 8012 0 930 PG-13 5828 1 darkski_hd "(HD) Dark Skies" Disk 0 04/01/13 10/01/13 0 0 0 0 0 0 1 1 0 16000000 H3 16:9 0 0

我创建的 PHP:

<?php

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

$theData = file_get_contents($filename) or die("Unable to retrieve file data");
    // echo nl2br($theData); //- This will print the entire text-wrapped line breaks.

$Ratings = ['G', 'PG', 'PG-13', 'R', 'NR', 'XXX']; // - This doesn't do anything yet.

if (preg_match('!"([^"]+)"!', $theData, $m)){

        echo $m[1];
}

?>

问题:我想返回 MOVIE TITLE : RATING,但到目前为止我的代码只返回一个电影标题,(HD) Identity Thief。我还有很长的路要走,所以任何指导将不胜感激。有没有办法严格按照收视率对电影标题进行排序:收视率?

此外,有没有办法让 PHP 脚本搜索目录和任何“.mov”文件扩展名的子文件夹,为每个文件运行脚本?

4

1 回答 1

0

这是我到目前为止所拥有的,我还没有得到排序部分,因为你没有说你想要它如何排序......

<?php
header("content-type: text/plain");
function getInfo($string){
    $Ratings = ['G', 'PG', 'PG-13', 'R', 'NR', 'XXX']; // Used in the loop below
    $split = preg_split("/\"(.+)\"/", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
    $string = $split[1];
    preg_match("/(".implode("|", $Ratings).")\s/", $split[0], $matches);
    $rating = $matches[0];
    return ["title" => $split[1], "rating" => $rating];
}


$string = <<<String
[Titles]
  hollywoodhd1 1 0 8046 0 919 PG-13 6712 1 identity_hd "(HD) Identity Thief" Disk 0 04/15/13 11/01/13 0 0 0 0 0 0 1 1 0 16000000 H3 16:9 0 0
  hollywoodhd2 3 0 8016 0 930 PG 5347 1 escapep_hd "(HD) Escape from Planet Earth" Disk 0 04/01/13 10/01/13 0 0 0 0 0 0 1 1 0 16000000 H3 16:9 0 0
  hollywoodhd3 1 0 8012 0 930 PG-13 5828 1 darkski_hd "(HD) Dark Skies" Disk 0 04/01/13 10/01/13 0 0 0 0 0 0 1 1 0 16000000 H3 16:9 0 0
String;
$titles = explode("\n", $string);
// Remove the first line
unset($titles[0]);

foreach($titles as $title){
    $info = getInfo($title);
    echo "{$info["title"]} : {$info["rating"]}\n";
}

然后,这是返回的输出:

(HD) 身份窃贼:PG-13
(HD) 逃离地球:PG
(HD) 黑暗天空:PG-13

于 2013-05-31T16:13:18.267 回答