I have a file name like this
1x5 Girl In The Flower Dress.mkv
It means Season 1 Episode 5
of In The Flower Dress
2x6.English,.Fitz.or.Percy.avi
It means Season 2 Episode 6
of English, Fitz or Percy
How to extract Season number ,Episode number and series name
Input
2x6.English,.Fitz.or.Percy.avi
Try this:
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
)
How about a more direct solution?
$title = '2x6.English,.Fitz.or.Percy.avi';
preg_match_all('~(?:(\d+)x(\d+)|(?!^)\G)[^\w\r\n,-]*\K[\w,-]++(?!$)~m', $title, $matches);
$matches = array_map('array_filter', $matches);
echo "Season {$matches[1][0]} Episode {$matches[2][0]} of ".implode(' ', $matches[0]);
Output:
Season 2 Episode 6 of English, Fitz or Percy
At first, I wanted to write: $out=preg_split('/[\\. x]/',$in,3,PREG_SPLIT_NO_EMPTY);
but because the title words can be dot delimited AND you don't want to capture the file suffix, I had to bin that method.
Barmar's method includes the file suffix, so that will require additional handling. Ravi's pattern isn't as refined as it could be.
Revo's method is inspired, but requires 4x as many steps as my pattern. Regex Demo Both of our methods require an additional function call to prepare the title. I find my method to be very direct and doesn't require any array filtering.
$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>";
}
Output:
Season 1 Episode 5 of Girl In The Flower Dress
Season 2 Episode 6 of English, Fitz or Percy
Use capture groups to get the parts of the string that match parts of the pattern.
preg_match('/(\d+)x(\d+)\s*(.*)/', '1x5 Girl In The Flower Dress.mkv', $match);
$match[1]
will be '1'
, $match[2]
will be '5'
, and $match[3]
will be 'Girl in the Flower Dress.mov'
.
You need to use \\d+
to match more than one digit in the season or episode number.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.