2011-04-03 101 views
1

我有一个脚本,输出状态更新和我需要编写一个脚本,自动改变像www.example.com成超链接在Twitter和Facebook这样做文本块。我可以在PHP中使用哪些函数?如果你知道一个教程,请发布它。如何在PHP中自动将URL转换为超链接?

+0

什么是 “HREF链接?”你是在谈论[Googlebot的shebang](http://stackoverflow.com/questions/3009380/)? – 2011-04-03 18:18:44

+1

@Matt Ball:可能是''。 – Gumbo 2011-04-03 18:20:52

回答

7
$string = " fasfasd http://webarto.com fasfsafa"; 

echo preg_replace("#http://([\S]+?)#Uis", '<a rel="nofollow" href="http://\\1">\\1</a>', $string); 

输出:

fasfasd <a rel="nofollow" href="http://webarto.com">webarto.com</a> fasfsafa 
+1

https呢? – Luka 2017-02-23 13:18:41

0

的最佳解决方案!

我想自动链接网页链接,并截断显示的URL文本,因为长URL在某些平台上已经脱离布局。

多少摆弄周围用正则表达式之后,我意识到该解决方案实际上是CSS - this site给出了使用CSS的白色空间的简单解决方案。

+0

您不应该仅仅给出另一个网站的链接作为答案,因为该网站将来可能会过时。相反,单击此答案上的“编辑”链接,并在此处包含解决方案的主要部分。请参阅:http://meta.stackexchange.com/q/8259 – 2012-10-19 22:17:21

-1

这里的工作职能

function AutoLinkUrls($str,$popup = FALSE){ 
if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches)){ 
    $pop = ($popup == TRUE) ? " target=\"_blank\" " : ""; 
    for ($i = 0; $i < count($matches['0']); $i++){ 
     $period = ''; 
     if (preg_match("|\.$|", $matches['6'][$i])){ 
      $period = '.'; 
      $matches['6'][$i] = substr($matches['6'][$i], 0, -1); 
     } 
     $str = str_replace($matches['0'][$i], 
       $matches['1'][$i].'</xmp><a href="http'. 
       $matches['4'][$i].'://'. 
       $matches['5'][$i]. 
       $matches['6'][$i].'"'.$pop.'>http'. 
       $matches['4'][$i].'://'. 
       $matches['5'][$i]. 
       $matches['6'][$i].'</a><xmp>'. 
       $period, $str); 
    }//end for 
}//end if 
return $str; } 
相关问题