我创建了一个我正在尝试解析数据的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];
}
?>
问题:我想返回电影标题:评分,但到目前为止我只返回一个电影片名,(HD)身份小偷。我还有很长的路要走,所以任何指导都会非常感激。有没有办法对电影标题进行排序:评级,严格按照评级?
此外,有没有办法让PHP脚本搜索目录,以及任何“.mov”文件扩展名的子文件夹,运行每个文件的脚本?
答案 0 :(得分: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)Dark Skies:PG-13