Posted By

fackz on 01/22/09


Tagged


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

jamesming
mihael
umang_nine
fackz
nb109
vali29
NyX
ninjacharlee


text to links


 / Published in: PHP
 

$text = "Visit www.jonasjohn.de :-)";

print text2links($text);

  1. function text2links($str='') {
  2.  
  3. if($str=='' or !preg_match('/(http|www\.|@)/i', $str)) { return $str; }
  4.  
  5. $lines = explode("\n", $str); $new_text = '';
  6. while (list($k,$l) = each($lines)) {
  7. // replace links:
  8. $l = preg_replace("/([ \t]|^)www\./i", "\\1http://www.", $l);
  9. $l = preg_replace("/([ \t]|^)ftp\./i", "\\1ftp://ftp.", $l);
  10.  
  11. $l = preg_replace("/(http:\/\/[^ )
  12. !]+)/i",
  13. "<a href=\"\\1\">\\1</a>", $l);
  14.  
  15. $l = preg_replace("/(https:\/\/[^ )
  16. !]+)/i",
  17. "<a href=\"\\1\">\\1</a>", $l);
  18.  
  19. $l = preg_replace("/(ftp:\/\/[^ )
  20. !]+)/i",
  21. "<a href=\"\\1\">\\1</a>", $l);
  22.  
  23. "/([-a-z0-9_]+(\.[_a-z0-9-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)+))/i",
  24. "<a href=\"mailto:\\1\">\\1</a>", $l);
  25.  
  26. $new_text .= $l."\n";
  27. }
  28.  
  29. return $new_text;
  30. }

Report this snippet  

You need to login to post a comment.