Convert links into clickable hyperlinks
Posted On at by milanConvert links into clickable hyperlinks
Description
A function to change an email address or URL into a clickable HTML hyperlink using eregi_replace.
The code
function makeClickableLinks($text) {
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1\\2', $text);
$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
'\\1', $text);
return $text;
}
// Usage
// Email address example
$text = "you@example.com";
echo makeClickableLinks($text);
echo "
";
// URL example
$text = "http://www.example.com";
echo makeClickableLinks($text);
echo "
";
// FTP URL example
$text = "ftp://ftp.example.com";
echo makeClickableLinks($text);
?>