Hello, I've built a content management system for my users and use the code below to convert things that look like URLs into clickable links.
Therefore www.followers.net becomes <a href="http://www.followers.net">www.followers.net</a>
Some of my users also wanted to type thier own html <a> </a> tags though. I think this is fine, but the problem is that the code below converts their url from:
<a href="http://www.followers.net">my site</a>
into:
<a href="<a href="http://www.followers.net">http://www.followers.net</a>">my site</a>
Of course this is not the desired effect.
Can someone recomend a way to make my regexps less aggressive? I haven't got this mastered yet.
// this function adapted from http://phpclasses.upperdesign.com/browse.html/package/372
// as part of the SDConvertText package
function activateLinks($str) {
// CONVERT URL like strings into clickable urls, including http, ftp and mailto
//
$str = eregi_replace('(((f|ht){1}t(p|ps)://)[-a-zA-Z0-9@:%+.~#?&//=]+)', '<a href="\1" target="blank">\1</a>', $str);
$str = eregi_replace('([:space:[{}])(www.[-a-zA-Z0-9@:%+.~#?&//=]+)', '\1<a href="http://\2" target="blank">\2</a>', $str);
$str = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})','<a href="mailto:\1">\1</a>', $str);
return $str;
}