February 21, 2007

Make Clickable Links

This is very small & useful function that finds & converts all the Email text, URL text to a clickable text. You just need to pass the full text & use the returned text.

Here is the code & usage example


### Use: To convert text parts(Email address, URLs) to clickable text
### Author: Akash Dwivedi
### Date: 27 June 2005

  1. function makeClickableLinks($text)
  2. {
  3. $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
  4. '\\1">\\1', $text);
  5. $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
  6. '\\1\\2">\\2', $text);
  7. $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
  8. '\\1">\\1', $text);
  9. return $text;
  10. }

# Example Usage
#Email address example
$text = "Mail me: you@example.com";
echo makeClickableLinks($text);

# URL example
$text = "My URL is: http://www.tech-akash.blogspot.com";
echo makeClickableLinks($text);

# FTP URL example
$text = "FTP is: ftp://ftp.example.com Here is the trailing text";
echo makeClickableLinks($text);
?>

I hope this is useful.

No comments: