2013-10-19 25 views
-1

好吧,我認爲我的問題的解決方案是使用REGEX,但我目前的知識阻礙了我實現我想要的結果。如何包裝<a href>我查詢中的所有URL

基本上,我如何在我的帖子/文本條目中使所有URL發生在被查詢時包裹在<a href>元素內。

當我echo $content;從數據庫中就會顯示爲:

this is a link http://somerandomsite.com, check it out! 

,但我怎樣才能使一個文本中的鏈接時,顯示將被包含在<a href>標籤。

id老實說,我不知道如何正確編寫語法。但這就是我想出的結果。

//get url from content 
$link = strstr($content, 'http://'); 

//insert <a href> for all $link inside $content 
//then display $content with all links wrapped in <href> 
+0

難道不是:「我有一個問題,我決定做一個正則表達式來解決這個問題現在我有2個問題。」?無論如何:如果你不知道語法應該是什麼,最合理的步驟是首先[發現](http://www.php.net/manual/en/pcre.pattern.php)。在使用文檔作爲參考嘗試了正則表達式之後再次提出您的問題。 – Sumurai8

回答

0
$a = 'this is a link http://somerandomsite.com, check it out!'; 

$a = preg_replace("/\b((?:http\:\/\/)|(?:www\.))([^ ,]+)/", "<a href=\"$1$2\">$1$2</a>", $a); 

echo $a; 
+0

謝謝你.. :) – bobbyjones

相關問題