2012-09-27 59 views
0

我有這種格式的字符串,我從這個string.The問題只想the-big-bang-theory在這個字符串有些話是可變長度刪除啓動字和結尾的單詞正則表達式

media-tv-quotes-2-the-big-bang-theory-94-toprated-2 
    ^       ^^ ^
---------------------------------------------------- 

我試圖用這個的代碼但沒有運氣。

$keywords = str_replace("/media-[a-z]-quotes-+/","", "media-tv-quotes-2-the-big-bang-theory-94-toprated-2"); 

有人知道我該怎麼做到這一點。

謝謝..

+0

您不能對正則表達式使用'str_replace'。使用'preg_replace'。 –

+0

是你想從第一個數字到第二個數字的邏輯嗎? – Matthew

+0

我也試過,但也沒有運氣.. –

回答

2

嘗試用下面的正則表達式:

$keywords = preg_replace("/^media-[a-z]+-quotes-\d+-(.*?)-\d+-.*$/", "$1", "media-tv-quotes-2-the-big-bang-theory-94-toprated-2"); 
+0

感謝它給我想要的輸出... –

+0

歡迎您。 ;-) – hsz

0

試試這個:

$keywords = preg_replace("/^(.*?)(the-big-bang-theory)(.*)$/i","$2", "media-tv-quotes-2-the-big-bang-theory-94-toprated-2"); 

the-big-bang-theory之前和之後捕捉任何字符。 然後只保留在替換the-big-bang-theory

+0

你不能在正則表達式中使用'str_replace'。嘗試'preg_replace' – Matthew

2

2號之間取任何東西:

$s = 'media-tv-quotes-2-the-big-bang-theory-94-toprated-2'; 
preg_match('~\d+-(.+?)-\d+~', $s, $matches); 
print_r($matches); 

問題acuress如果你的電影片名中有數字,在這種情況下使用:

$s = 'media-tv-quotes-2-the-big-bang-theory-94-toprated-2'; 
preg_match('~^media-\w+-\w+-\d+-(.+?)-\d+-\w+-\d+$~i', $s, $matches); 
print_r($matches); 
相關問題