经测试代码如下:
<?PHP
/**
* 字符串中的url替换成可点击的链接,给链接添加nofollow
*
* @param
* @arrange (512.笔记) jb51.cc
**/
function replace_urls($string,$rel = 'nofollow'){
$host = "([a-z\d][-a-z\d]*[a-z\d]\.)+[a-z][-a-z\d]*[a-z]";
$port = "(:\d{1,})?";
$path = "(\/[^?<>\#\"\s]+)?";
$query = "(\?[^<>\#\"\s]+)?";
return preg_replace("#((ht|f)tps?:\/\/{$host}{$port}{$path}{$query})#i","<a href=\"$1\" rel=\"{$rel}\">$1</a>",$string);
}
/*** 来自:编程之家 jb51.cc(jb51.cc) ***/
?>