简体   繁体   中英

PHP Code replace whit Regular expressions

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?

You can use:

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

Code Demo

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

键盘上链接。

2 ways without regular expressions

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM