kamron shaw kamron shaw - 3 months ago 9
PHP Question

Extract a particular character from a string

I want to extract the episode number from the name of TV Series.

e.g. If it is "Game of Thrones S06E01", Then I should get 01.

What I am trying is :

  1. First see if the string contains something like "S__E__"(e.g "Blacklist S01E05")

  2. If yes, then see what's there after "E" or "e", and extract it.

But due to variation in types of inputs, I can get different names to check like :

  1. Blacklist S01E01

  2. Blacklist s01e01 (small "e" and "s")

  3. Blacklist S1E1

  4. Blacklist S01E01.mkv

  5. Se7en S01E01

and so on...

Jan Jan

Praise the power of regular expressions then:


See a demo on regex101.com.

In PHP this would be:


$data = <<<DATA
Blacklist S01E01
Blacklist s01e01 (small "e" and "s")
Blacklist S01E01.mkv
Se7en S01E01

$regex = '~\b(?i)S\d+E(\d+)(?i-)\b~';
preg_match_all($regex, $data, $matches);

And see a demo on ideone.com.

Updated: To do it in jQuery/JavaScript, you need to change the inline modifiers:


See a demo on regex101.com (and mind the modifiers!).