21,886
社区成员
发帖
与我相关
我的任务
分享
$s = '“文字www.baidu.com文字
www.html.com
文字http://www.baidu.com
https://www.google.com”';
echo make_clickable($s);
function make_clickable($str) {
$str = preg_replace('`([^"=\'>])((http://|https://|ftp://|ftps://|www.)[^\s< ]+[^\s<\.)])`ie',"_link('\\1','\\2')" , $str);
return $str;
}
function _link($text,$link){
if(explode('.',$link)[0]=='www'){
$link='http://'.$link;
}
return $text.'<a href="'.$link.'" target="_blank" rel="nofollow">'.$link.'</a>';
}