1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
function makeClickableLinks($text) {
# http
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text);
$text = eregi_replace('((([[:space:]()[{}])(f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $text);
$text = eregi_replace('(^|[\n ])((www.|ftp.)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $text);
# https
$text = eregi_replace('(((^|[\n ])(f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text);
$text = eregi_replace('((([[:space:]()[{}])(f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text);
# Mail
$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $text);
return $text;
}
Refactorings
No refactoring yet !
This works for http:// https:// ftp:// and for email addresses whether they are in the middle of the text or the beginning or the end - images even survive if it's a html image tag in the text but the embed html tag gets destroyed and not displayed properly.
Also it does not always capture when multiple entries exist in the code.
Anyone have a good fix for either of those two problems?