2013-05-14 59 views
1

這是我的if語句,將採取從源代碼這個例子HREF:在PHP中使用正則表達式從網址中刪除一個字

href="/toronto/2013/05/14/the-airborne-toxic-event/4296/" 

從源代碼

if ($text=preg_match('/href="([^"]*)"/', $text, $matches)) 
{ 
    $output=$matches[1]; 
    return $output; 
} 

,並返回

/toronto/2013/05/14/the-airborne-toxic-event/4296/ 

我想要返回該網址,而不是"/toronto""/toronto/"(我不確定哪一個我會需要)

如果你能告訴我這樣做的正則表達式,我會非常感激。 感謝

+1

考慮使用'str_replace'而不是正則表達式。這個用例很簡單,它不是真的需要。 – pauljz 2013-05-14 15:23:50

回答

1

使用preg_replace

return preg_replace('~^/?toronto/~', '', $output); 

如果你相信 「多倫多/」 沒有其他地方出現的字符串中,你可以使用str_replace

return str_replace('/toronto', '', $output); 

這假設總會有一個主要的斜線。

相關問題