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

moqu8 2018-10-10

1681 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
新站优化中!部分功能尚未完善,敬请谅解!
支持中心