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] )
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:
^
- start of string(?<name>.*?)
- a named capturing group matching any 0+ chars other than line break symbols, as few as possible, up to the first....\.S
- .S
substring of literal chars (?<season>\d+)
- a "season" named capturing group matching 1+ digitsE
- a literal char E
(?<episode>\d+)
- an "episode" named capturing group matching 1+ digits