我有一个这样的文件名
1x5 Girl In The Flower Dress.mkv
这意味着Season 1 Episode 5
In The Flower Dress
2x6.English,.Fitz.or.Percy.avi
这意味着Season 2 Episode 6
English, Fitz or Percy
如何提取季号,集数和系列名称
输入
2x6.English,.Fitz.or.Percy.avi
试试这个:
preg_match("/(d*)x(d*).?(.*).(.*)/", $input, $output_array);
output_array
array(
0 => 2x6.English,.Fitz.or.Percy.avi
1 => 2 // Season
2 => 6 // Episode
3 => English,.Fitz.or.Percy // title
4 => avi
)
更直接的解决方案怎么样?
$title = '2x6.English,.Fitz.or.Percy.avi';
preg_match_all('~(?:(d+)x(d+)|(?!^)G)[^wrn,-]*K[w,-]++(?!$)~m', $title, $matches);
$matches = array_map('array_filter', $matches);
echo "Season {$matches[1][0]} Episode {$matches[2][0]} of ".implode(' ', $matches[0]);
输出:
Season 2 Episode 6 of English, Fitz or Percy
> 起初,我想写: $out=preg_split('/[. x]/',$in,3,PREG_SPLIT_NO_EMPTY);
但是因为标题词可以点分隔并且您不想捕获文件后缀,所以我不得不将该方法装箱。
Barmar的方法包括文件后缀,因此需要额外的处理。 拉维的模式并不像它可能的那样精致。
Revo 的方法受到启发,但需要的步骤是我的模式的 4 倍。 正则表达式演示 我们的两个方法都需要额外的函数调用来准备标题。 我发现我的方法非常直接,不需要任何数组过滤。
$input[]='1x5 Girl In The Flower Dress.mkv';
$input[]='2x6.English,.Fitz.or.Percy.avi';
foreach($input as $in){
preg_match('/(d+)x(d+)[ .](.+)..+/',$in,$out);
echo "<div>";
echo "Season $out[1] Episode $out[2] of ",str_replace('.',' ',$out[3]);
echo "</div>";
}
输出:
Season 1 Episode 5 of Girl In The Flower Dress
Season 2 Episode 6 of English, Fitz or Percy
使用捕获组获取与模式部分匹配的字符串部分。
preg_match('/(d+)x(d+)s*(.*)/', '1x5 Girl In The Flower Dress.mkv', $match);
$match[1]
将被'1'
,$match[2]
将被'5'
,$match[3]
将被'Girl in the Flower Dress.mov'
。
您需要使用d+
来匹配季号或剧集编号中的多位数字。