2011-09-20 41 views
-1

可能重複:
replace any url's within a string of text, to clickable links with phpPHP使用preg_replace創建鏈接?

只是一個快速的問題,當我喜歡張貼鏈接http://www.buddyweb.me它只會出現這樣的,但,但T的不是全自動的聯繫。所以,我怎麼能取代http://www.buddyweb.me<a href = "http://www.buddyweb.me">Google</a>

任何建議都apreciated,謝謝:)

+3

http://stackoverflow.com/questions/1798912/replace-any-urls-within-a-string-of-text-to-clickable-links-with-php – Rijk

+0

請說明你想要什麼是處理一個頁面,並將網址變成鏈接或只是回聲從PHP的鏈接? – SuitedSloth

+0

謝謝:)這麼多! – frankmeacey

回答

1

突出部分好像here

function clickable($url){ 
    $url         = str_replace("\\r","\r",$url); 
    $url         = str_replace("\\n","\n<BR>",$url); 
    $url         = str_replace("\\n\\r","\n\r",$url); 

    $in=array(
    '`((?:https?|ftp)://\S+[[:alnum:]]/?)`si', 
    '`((?<!//)(www\.\S+[[:alnum:]]/?))`si' 
    ); 
    $out=array(
    '<a href="$1" rel=nofollow>$1</a> ', 
    '<a href="http://$1" rel=\'nofollow\'>$1</a>' 
    ); 
    return preg_replace($in,$out,$url); 
} 
+0

似乎像他想要的矯枉過正,也許我不明白他想要什麼。 –

+0

他用php和preg-replace標記了它。我以爲他想用一些發佈的數據中的鏈接替換網址。 – NiematojakTomasz

0

無需浸漬料替換,正好連接在你的鏈接可變因素。

<? 

$yourlink = "http://www.buddyweb.me"; 
$yourDescriptor = "Google"; 

$linkedlink = "<a href=\"".$yourlink.">$yourDescriptor</a>"; 

echo $linkedlink; 

?> 
+0

我想他想要的是處理整個頁面並鏈接到URL。從評論鏈接的SO問題有答案。 – SuitedSloth

+0

啊,這樣更有意義。 –

1
$replaced = preg_replace('/(http[s]?:\/\/[^\s]*)/i', '<a href="$1">$1</a>', $url); 
0

呼叫東西​​,你怎麼樣,將其返回。

<?php 
$link = "http://stackoverflow.com"; 
$name = "Stack Overflow"; 

echo href($link, $name); 

function href($link, $name){ 
$link = "<a href=\"".$link.">$name</a>"; 
return $link; 
} 
?>