PHP在一个输入中分隔两个不同的部分

I'm working on a PHP based application extension that will extend a launcher style app via the TVRage API class to return results to a user wherever they may be. This is done via Alfred App (alfredapp.com).

I would like to add the ability to include show name followed by S##E##: example: Mike & Molly S01E02

The show name can change, so I can't stop it there, but I want to separate the S##E## from the show name. This will allow me to use that information to continue the search via the API. Even better, if there was a way to grab the numbers, and only the numbers between the S and the E (in the example 01) and the numbers after E (in the example 02) that would be perfect.

I was thinking the best function is strpos but after looking closer that searches for a string within a string. I believe I would need to use a regex to correctly do this. That would leave me with preg_match. Which led me to:

$regex = ?;
preg_match( ,$input);

Problem is I just don't understand Regular Expressions well enough to write it. What regular expression could be used to separate the show name from the S##E## or get just the two separate numbers?

Also, if you have a good place to teach regular expressions, that would be fantastic.

Thanks!

Regex:

$text = 'Mike & Molly S01E02';
preg_match("/(.+)(S\d{2}E\d{2})/", $text, $output);
print_r($output);

Output:

Array
(
    [0] => Mike & Molly S01E02
    [1] => Mike & Molly 
    [2] => S01E02
)

If you want the digits separately:

$text = 'Mike & Molly S01E02';
preg_match("/(.+)S(\d{2})E(\d{2})/", $text, $output);
print_r($output);

Output:

Array
(
    [0] => Mike & Molly S01E02
    [1] => Mike & Molly 
    [2] => 01
    [3] => 02
)

Explanation:

. --> Match every character

.+ --> Match every character one or more times

\d --> Match a digit

\d{2} --> Match 2 digits

The parenthesis are to group the results.

www.regular-expressions.info is a good place to learn regex.

You can turn it around and use strrpos to look for the last space in the string and then use substr to get two strings based on the position you found.

Example:

$your_input = trim($input);    // make sure there are no spaces at the end (and the beginning)
$last_space_at = strrpos($your_input, " ");
$show = substr($your_input, 0, $last_space_at - 1);
$episode = substr($your_input, $last_space_at + 1);