2014-06-18 56 views
0

使用AutoFormat.Linkify HTMLpurifier轉換文本,如http://www.example.com into鏈接。 但很多人在沒有協議的情況下編寫鏈接,例如www.example.comexample.com。反正用HTMLpurifier也可以將它們轉換成鏈接嗎?htmlpurifier添加缺少的url協議

回答

0

我知道沒有辦法讓HTMLpurifier做到這一點。但是,您應該通過將http://添加到每個鏈接來獲得這項工作,但不包含它。您可以使用正則表達式來執行此操作。

preg_replace('#\s((https?|ftp)\:\/\/)?([a-z0-9-.]*)\.([a-z]{2,4})\s#', 
       ' http://${3}.${4} ', $html); 

測試正則表達式here

例子:

test example.com test<br> 
test www.example.com test<br> 
test http://example.com test 

成爲

test http://example.com test 
test http://www.example.com test 
test http://example.com test 

現在HTMLpurifier應該做正確的事。

+0

謝謝。但是,如果我是一篇很長的文章,例如一篇文章或一篇博客文章,那麼這不是一個好主意嗎? – oozrafa

+0

由於運行時間?我認爲這不應該是一個問題。我用這種方法使我的php代碼更好地被doxygen記錄下來。所以正則表達式在大約兩秒鐘內運行10000行代碼。最好的做法是用您期望的數據作爲輸入來運行性能測試。 – AbcAeffchen