Search code examples
phpregexpreg-match

Extract Characters Before And After Middle Symbol regex and rest string


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


Solution

  • 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.