2

I have my site on series, usually all series are divided by seasons and episodes, I have the following string: The Mentalist S05E14

where **S05E14** is like **Episode 14 Season 05** I need to change the expression:

**S05E14** 
   for 
**05x14**

Also, for any season and episode, so I would remove "S" and replace the "E" for "x"

How can I do this using regular expressions or perhaps otherwise?

4 Answers 4

3

You can use:

$repl = preg_replace('/S(\d+)E(\d+)/i', '$1x$2', 'S05E14');

Code Demo

Sign up to request clarification or add additional context in comments.

Comments

1
echo preg_replace( '/S(\d+)E(\d+)/', '\1x\2', $str );

Link on Codepad.

3 Comments

This answer is exactly like mine and was entered about 2 mins after my answer :)
@anubhava I used the entire name of episode. :P
/S(\d+)E(\d+)/i this is the regex I had used and you can see your answer now :)
0

2 ways without regular expressions

$e = 'S05E14';
echo implode( 'x', explode( 'E', substr( $e, 1 ) ) );
echo str_replace( array( 'S', 'E' ), array( '', 'x' ), $e );

Comments

0

Try this:

$string_val="The Mentalist S05E14";
$data = explode(' ', $text);
$count = count($data);
$last_word = $data[$count-1];
$newStr = str_replace("S", "", $last_word);
$newStr = str_replace("E", "x", $newStr);

1 Comment

The string is bigger than just "S05E14". This will replace any S and E.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.