2012-12-13 35 views
-1

我不擅长正则表达式,我需要一些帮助。正则表达式替换php中的链接

我这下面的链接链接:

http://www.mydomain.com/1/1/5/1/some-name-123-115194_7_9.jpg

应该是正则表达式什么用php得到象下面这样:

http://www.mydomain.com/1/1/5/1/115194_7_9.jpg

这就是我所拥有的Ë试过到目前为止:

preg_match_all('/(\d+)(\w+)/', $str,$matches); 
+0

[?你尝试过什么(http://www.whathaveyoutried.com/)查看请[FAQ](http://stackoverflow.com/faq)。 –

+0

你有什么尝试?在这里,[使用这个在线正则表达式测试我刚刚通过谷歌搜索](http://regexpal.com/) – kojiro

+0

我试过这个。 preg_match_all('/(\ d +)(\ w +)/',$ str,$ matches); –

回答

1

使用preg_replace

$url = "http://www.mydomain.com/1/1/5/1/some-name-123-115194_7_9.jpg"; 

echo preg_replace('#(.+/).+-(.+)#','$1$2',$url) 

>>> http://www.mydomain.com/1/1/5/1/115194_7_9.jpg 

Rexplanation:

(.+/) # Match everything upto the last/and capture 
.+-  # Match upto the last - 
(.+) # Match and capture everything else 
     # Replace with 
$1$2 # The first and second capture groups 
+0

但它应该是http://www.mydomain.com/1/1/5/1/115194_7_9.jpg。 –

+0

我的不好,看到编辑:) –

+1

谢谢你,我的朋友:) –