+ Almost all quality improvement comes via simplification of design, manufacturing... layout, processes, and procedures.

July 30, 2010, 5:13 pm

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)
  1. function makeClickableLinks($text) {
  2.  
  3. $text = eregi_replace(’(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)’,
  4.  
  5.     ’<a href="1">1</a>’, $text);
  6.  
  7.   $text = eregi_replace(’([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)’,
  8.  
  9.     ’1<a href="http://2">2</a>’, $text);
  10.  
  11.   $text = eregi_replace(’([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})’,
  12.  
  13.     ’<a href="mailto:1">1</a>’, $text);
  14.  
  15. return $text;
  16.  
  17. }
  18.  
  19. // Usage
  20.  
  21. // Email address example
  22.  
  23. $text = "you@example.com";
  24.  
  25. echo makeClickableLinks($text);
  26.  
  27. echo "
  28.  
  29. ";
  30.  
  31. // URL example
  32.  
  33. $text = "http://www.example.com";
  34.  
  35. echo makeClickableLinks($text);
  36.  
  37. echo "
  38.  
  39. ";
  40.  
  41. // FTP URL example
  42.  
  43. $text = "ftp://ftp.example.com";
  44.  
  45. echo makeClickableLinks($text);

Posted by Anthony under PHP | Comments (0)

No Comments »

RSS feed for comments on this post. | Trackback

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

*
To prove that you're not a bot, enter this code
Anti-Spam Image