2013-02-18 34 views
2

我有我的一系列網站,通常都是系列是由季節和情節劃分,我有以下字符串: 心計S05E14PHP代碼替換白衣正則表達式

where **S05E14** is like **Episode 14 Season 05**我需要改變的表達:

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

此外,對於任何季節和情節,所以我會刪除「S」和更換「E」爲「×」

我怎麼可以這樣使用正則表達式或以其他方式也許做?

回答

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

Codepad鏈接。

+1

這個答案是完全像我和我的答案:) – anubhava 2013-02-18 04:43:37

+0

@anubhava我用情節的全名後,進入了約2分鐘。 :P – hjpotter92 2013-02-18 04:55:35

+0

'/ S(\ d +)E(\ d +)/ i'這是我用過的正則表達式,你現在可以看到你的答案:) – anubhava 2013-02-18 05:07:42

3

您可以使用:

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

Code Demo

0

2種方式,而不正則表達式

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

試試這個:

$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); 
+2

字符串比'S05E14''大。這將取代任何S和E. – 2013-02-18 04:27:26