Posted By


jspicher on 10/09/09

Tagged


Statistics


Viewed 580 times
Favorited by 1 user(s)

make_clickable($text)


/ Published in: PHP
Save to your folder(s)

Used to convert URLs in a string into links


Copy this code and paste it in your HTML
  1. function make_clickable($text){
  2. $ret = " " . $text;
  3. $ret = preg_replace("#([\n ])([a-z]+?)://([^,\t \n\r]+)#i", "\\1<a href=\"\\2://\\3\" target=\"_blank\">\\2://\\3</a>", $ret);
  4. $ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^,\t \n\r]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\" target=\"_blank\">www.\\2.\\3\\4</a>", $ret);
  5. $ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
  6. $ret = substr($ret, 1);
  7. return($ret);}

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.