2011-09-20 46 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; 
} 
?>