PHP给文本内所有网址自动增加超链接函数代码 [复制链接]

moqu8 2018-10-10

1723 0
代码如下:

 function autolink($foo) {
$foo = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\1" target=_blank rel=nofollow>\1</a>', $foo);
if (strpos($foo, "http") === FALSE) {
$foo = eregi_replace('(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\1" target=_blank rel=nofollow >\1</a>', $foo);
} else {
$foo = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\1<a href="\2" target=_blank rel=nofollow >\2</a>', $foo);
}
return $foo;
}


此代码不足之处在于,如果有多个链接,比如:

http://we.moqu8.com
https://we.moqu8.com
we.moqu8.com
则只有带http://开头的会自动增加上超链接(所有的http://都会加上)。

最新回复 (0)
返回
支持中心
邮箱:winkill2012@qqcom

历史资源已不再提供任何技术支持和售后服务,客服仅处理无法下载或支付相关问题,资源使用或异常问题请自行解决!
支持中心