2011-05-22 63 views
2

我有這條正則表達式,我正在使用這個正則表達式來創建一個從輸入到textarea中的URL的可點擊鏈接。我沒有編寫代碼,也不知道如何修改它,以便在文本以http或https開頭時創建鏈接。如何修改此正則表達式以包含https?

$html = preg_replace('"\b(http://\S+)"', '<a href="$1">$1</a>', stripslashes($rows['body'])); 

回答

4

?添加到正則表達式使前面的字符可選。

$html = preg_replace('"\b(https?://\S+)"', '<a href="$1">$1</a>', stripslashes($rows['body'])); 
+0

謝謝!那樣做了。 – EmmyS 2011-05-23 13:45:46

2

更換

\b(http://\S+) 

有:

\b(https?://\S+) 

一起:

$html = preg_replace('"\b(https?://\S+)"', '<a href="$1">$1</a>', stripslashes($rows['body'])); 
0

沒有正則表達式的專家,所以這可能工作:

$html = preg_replace('"\b(http://\S+|https://\S+)"', '<a href="$1">$1</a>', stripslashes($rows['body']));