替换数字和单词

how can i change string from this pattern 'name season 1 episode 2', to = 'name S01E02' - but if there is episode more than 10 like that 'name season 1 episode 15' only the season will change, i have try to do so:

<?php
$trans = array(
    ' season'   =>      ' S',
    ' 1'        =>      ' 01',
    ' 2'        =>      ' 02',
    ' 3'        =>      ' 03',
    ' 4'        =>      ' 04',
    ' 5'        =>      ' 05',
    ' 6'        =>      ' 06',
    ' 7'        =>      ' 07',
    ' 8'        =>      ' 08',
    ' 9'        =>      ' 09',
    ' episode'      =>      ' E'
);
$q = strtr($q, $trans);


$trans = array(
    'S '        =>      'S',
    ' E '       =>      'E'
);
$q = strtr($q, $trans);
?>
$string = 'name season 1 episode 2';
if (preg_replace('/(.*?) season (\d+) episode (\d+)/', $string, $matches) {
     $new_name = sprintf('%s S%02dE%02', $matches[1], $matches[2], $matches[3]);
}

should make new_name = 'name S01E02', assuming your names are consistent with the formatting and internal structure.

$original_string = "some_thing season 1 episode 9";
preg_replace("/([^ ]*) season ([\d]) episode ([\d])/", "$1 S0$2E0$3", $original_string);