2013-01-23 52 views
-3

可能重複:
Linkify PHP text自由文本中創建URL鏈接

您好我有一個字段從包含HTTP鏈接,以及普通文本的數據庫。

我需要找到一種方法來顯示這些信息,使用PHP來顯示文本和適用的鏈接。

,以下是我的SQL查詢:

$sql = 
    "select bw_supporting_doc 
    from cm3rm2 with(nolock) 
    where number = '$id'"; 

這條SQL語句的輸出是:

Please find below the link to the FSS: http://blahblack.html

我需要顯示http://blahblack.html因爲此刻它會顯示一個鏈接作爲正常文本。

謝謝!

+0

[你嘗試過什麼?](http://whathaveyoutried.com) – 2013-01-23 08:45:03

回答

0
function hyperlink($text){ 
    $text = preg_replace("#(http://)+?([.]?[a-zA-Z0-9_/-])*#", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $text); 
    return $text; 
} 

這可能做的工作

用法示例: $str = 'this is a random string which contains a link http://www.yahoo.com';

echo hyperlink($str);

+0

'ereg'以來一直5.3.0棄用 - 使用PCRE http://php.net/manual/en/ book.pcre.php – meouw

+0

是的,我知道了,用preg_replace代替它。我需要現在投票:( –

+0

您需要先添加分隔符 – meouw