phpregexstring-parsing

Parse television series filename string to get show name, series number, and episode number


I've having trouble splitting this string into components. The example string I have is Criminal.Minds.S10E22.WEB-DL.x264-FUM[ettv]. I'm trying to split it into the following: Criminal Minds, 10, 22.

Though I've dabbled a bit in perl regex, the php implementation is confusing me.

I've written the following:

$word = "Criminal.Minds.S10E22.WEB-DL.x264-FUM[ettv]";
// First replace periods and dashes by spaces
$patterns = array();
$patterns[0] = '/\./';
$patterns[1] = '/-/';
$replacement = ' ';
$word = preg_replace($patterns, $replacement, $word);
print_r(preg_split('#([a-zA-Z])+\sS(\d+)E(\d+)#i', $word));

Which outputs Array ( [0] => Criminal [1] => WEB DL x264 FUM[ettv] )


Solution

  • Use matching rather than splitting if the string is always in this format:

    $word = "Criminal.Minds.S10E22.WEB-DL.x264-FUM[ettv]";
    preg_match('~^(?<name>.*?)\.S(?<season>\d+)E(?<episode>\d+)~', $word, $m);
    print_r($m);
    

    See the PHP demo

    Then, you can access the name, season and episode values using $m["name"], $m["season"] and $m["episode"].

    Pattern details: