2009-12-08 120 views
0
它們轉化爲錨標籤
http://stackoverflow.com 

應該成爲查找字符串中的所有鏈接,並使用PHP

<a href="http://stackoverflow.com">http://stackoverflow.com</a> 


編輯:這將是巨大的,如果從原來的字符串錨標籤保持完好。

+0

http://stackoverflow.com/questions/1798912/replace-any-urls-within-a-string-of-text-to-clickable-links-with-php – Sampson 2009-12-08 04:41:58

+0

http://stackoverflow.com/questions/1038284/PHP的解析鏈接的電子郵件中 – Sampson 2009-12-08 04:42:34

回答

2

約翰格魯伯剛剛發佈了一個有趣的正則表達式例子來捕捉網址: Daring Fireball regex to get URLs

要切入正題,在(自由的,長),他選擇模式是:

\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))) 

這假定捕獲以下所有:

http://foo.com/blah_blah 
http://foo.com/blah_blah/ 
(Something like http://foo.com/blah_blah) 
http://foo.com/blah_blah_(wikipedia) 
(Something like http://foo.com/blah_blah_(wikipedia)) 
http://foo.com/blah_blah. 
http://foo.com/blah_blah/. 
<http://foo.com/blah_blah> 
<http://foo.com/blah_blah/> 
http://foo.com/blah_blah, 
http://www.example.com/wpstyle/?p=364. 
http://✪df.ws/123 
rdar://1234 
rdar:/1234 
http://userid:[email protected]:8080 
http://[email protected] 
http://[email protected]:8080 
http://userid:[email protected] 
http://example.com:8080 x-yojimbo-item://6303E4C1-xxxx-45A6-AB9D-3A908F59AE0E 
message://%[email protected]%3e 
http://➡.ws/䨹 
www.➡.ws/䨹 
<tag>http://example.com</tag> 
Just a www.example.com link. 

所以,你會使用模式的東西,如preg_filter,然後迭代OV呃以某種方式返回返回的數組。我猜。我討厭正則表達式。

相關問題