2011-12-22 76 views
1

現在 我使用的preg_replace的網址和鏈接

$content = preg_replace('@(https?://([-\w\.]+)+(:\d+)?((/[\w/_\.%\-+~]*)?(\?\S+)?)?)@', '<a href="$1" target="_blank">$1</a>', $content); 

用於替換鏈接URL,但它並沒有與像#一些符號和其他許多

和我也想工作如果內容看起來像這樣

<a href="http://www.abc.com/">http://www.abc.com/</a> 

然後preg_replace跳過這個,否則它會重複相同併產生錯誤的結果。

+2

這個問題被問了很多。嘗試搜索:「php linkify url」 – ridgerunner 2011-12-22 17:57:08

+0

或者如果你絕望:http://txt2re.com/ – user978122 2011-12-22 18:27:30

回答

0

爲什麼不直接尋找任何以http://或https://開頭的東西,直到任何空格字符?

https?://[^\s]+ 

這顯然是相當寬容,唯一的問題是,你可能得到一些誤報。