2012-07-18 65 views
2

我想使用的preg_replace只能更換的preg_replace鏈接

"http://domain.com/dateiIe6EHOnzyl/_DSC7290.jpg.htm" 

"http://domain.com/dateiIe6EHOnzyl.htm" 

P/S:我需要只使用了preg_replace

+1

在哪裏?在一個HTML字符串中,URL重寫是什麼? – DaveRandom 2012-07-18 11:56:50

+0

@DaveRandom,標籤.. ehhm!順便提一下,質疑upvoter,對你感到羞恥! – Adi 2012-07-18 11:57:09

+2

http://www.regular-expressions.info/是學習正則表達式的好開始。 – 2012-07-18 11:58:43

回答

0

沒有的preg_replace嘗試() ,但是使用explode()和str_replace(),如下例所示:

$str = 'http://domain.com/dateiIe6EHOnzyl/_DSC7290.jpg.htm'; 
$lastpart = end(explode('/', $str)); 
$str2 = str_replace('/'.$lastpart, '.htm', $str); 
echo $str2;   // http://domain.com/dateiIe6EHOnzyl.htm 
+0

謝謝,但我只使用preg_replace – Thoman 2012-07-18 12:46:08

0
$str = 'http://domain.com/dateiIe6EHOnzyl/_DSC7290.jpg.htm'; 
$str = preg_replace('/(.+)\/.+\.(.+)/','$1.$2',$str); 
print $str;