2012-09-27 61 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); 
相关问题