2015-01-05 96 views
-3

我這裏創建一個可點擊的鏈接的功能:PHP使得點擊鏈接

function makeClickableLinks($text) { 
     $notIncludedonLink = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $text); // removing not included on the link 
     $urlLink = str_replace($notIncludedonLink,'',$text); 
     $finalText = str_replace($urlLink,'<a href="'.$urlLink.'" target="_blank">'.$urlLink.'</a>',$text); 
     return $finalText; 
    } 

,而不是返回簡單的可點擊的鏈接,但是:

http://docs.google.com/ 

它顯示:

<a href="http://docs.google.com/" target="_blank">http://docs.google.com/</a> 

我嘗試使用htmlentities,但它不起作用。

這裏有一個JS代碼,將數據發送到服務器:

function checkNewLink() { 
var latestId = $("input[name=latestLink]").val(); 
$('.newReply').load("links/ajax.php?action=newreply&msgid=<?php echo $msgId; ?>&latestid=" + latestId); 
} 
setInterval("checkNewLink()", 200); 

其中latestId包含輸入鏈接。它將被髮送到ajax.php。每200毫秒,它會檢查是否有新的輸入鏈接。

+0

這並不理解什麼是你想在這裏實現。請更詳細地解釋您的問題 –

+0

「$ text」包含什麼內容? – Priyank

+0

它運作良好。你怎麼了?你是否像'text/html'而不是'text/plain'那樣正確地設置你的輸出?我們可以看到完整的代碼嗎? –

回答

2
<?php 
function makeClickableLinks($text){ 
    return preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()[email protected]:%_+.~#?&;//=]+)!i', '<a href="$1" target="_blank">$1</a>', $text); 
} 

echo makeClickableLinks('test http://docs.google.com/ test'); 

輸出代碼(http://codepad.org/EZE1HFZ4

測試http://docs.google.com/測試

AFTER UPDATE

變化

setInterval("checkNewLink()", 200); 

setInterval(function(){ checkNewLink() }, 200); 

閱讀setInterval()方法

+0

這是工作的人。但結合jQuery和其他一些代碼,事實並非如此。我會粘貼一些JS和jQuery代碼片段 – ajdeguzman

+0

好的,更新你的答案... –

+0

@SimoneNigro小提琴不起作用。一探究竟。 –