2014-04-25 45 views
0

我創建了一個自動檢測鏈接的函數。它工作得很好,除非一個HTML標籤剛好在一個鏈接之後開始(沒有空格)。自動鏈接檢測器(PHP + RegEx)

實施例:

http://www.google.com is cool 

變得

<a href="http://www.google.com" target="_blank">www.google.com</a> is cool 

http://www.google.com<br />is cool 

(無空間HTTP後://在真實)變爲

<a href="http://www.google.com<br /" target="_blank">www.google.com<br /</a> is cool 

我的正則表達式首先不會停止<,但不允許< ...

我該如何解決這個問題?或者我怎樣纔能有效地排除<

這裏是我的功能:

function detect_linkg($str){   
    return preg_replace('#(https?://)([\w\d.&:\#@%/;$~_?\+-=]*)#','<a href="$1$2" target="_blank">$2</a>',$str); 
} 

感謝您的幫助!

回答

1

我認爲這個問題是在這裏:+-=

這實際上創建了一個範圍,像a-z,但在這種情況下,從+=,如果你看ascii圖表,你看到的是包括<(但不是>)。

只需逃脫-如果你想字面:+\-=

+0

謝謝,這就是解決方案! –