Search code examples
phpregexpreg-match

php preg_match and regex regular expression


I want to use the regex:

/(.*)[.\s][sS](\d{1,20})[eE](\d{1,100}).*/i

to filter for the title of a tv series. (e.g. The Big Bang Theory S04E05) In order to remove the episode string (S04E05).

I've tested my regex with http://www.phpliveregex.com/ and everything works fine. But including it to my website, I'll get the whole title including the episode string. The return value of preg_match is 0.

My Code:

$ret=preg_match("/(.*)[.\s][sS](\d{1,20})[eE](\d{1,100}).*/i", $title,$output);
if($ret==1){
    $title_without=$output[1];
}

Solution

  • Note that inside a double-quoted string, you need to use double backslash to escape regex shorthand classes.

    You can use your regex inside a preg_replace function inside single quotes so that you do not have to double backslashes:

    $title= "The Big Bang Theory S04E05";
    $ret=preg_replace('/^(.*)[.\s]s\d{1,20}e\d{1,100}(.*)/i', '\1\2', $title);
    echo $ret;
    

    See IDEONE demo. Result: The Big Bang Theory.

    The back-references \1\2 will restore the substrings before and after the episode substring.

    Since you are using /i modifier, you need not use [eE] or [Ss], just use single letters in any case.

    To return the substring before the episode and the episode substring itself, just use the capturing groups with preg_match like here:

    $title= "The Big Bang Theory S04E05";
    $ret=preg_match('/^(.*)[.\s](s\d{1,20}e\d{1,100})/i', $title, $match);
    echo $match[1] . PHP_EOL; // => The Big Bang Theory
    echo $match[2];           // => S04E05
    

    See another demo