PHP Function: Make non linked links clickable
December 16, 2008
Recently I came across a problem making a twitter plugin. I needed to make links which had no “a” tag into links. You can do this progamaticly and here is the solution:
If you are going to use this script please download from here: links.php.txt
Code (php)
-
function makeClickableLinks($text) {
-
-
$text = eregi_replace(’(((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(’([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})’,
-
-
’<a href="mailto:1">1</a>’, $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);
Posted by Anthony under PHP | Comments (0)