Page 1 of 1

Active Link via database query

Posted: Sun Aug 14, 2005 10:36 am
by Addos
When I insert an email or web address into my database I would like this to automatically display in the browser as an active link. I’m very new to this so I wonder if anybody can tell or show me how this is done. I think there is a regex or something similar needed to run on the page but as I say I’m not sure where to start.
Thanks a mil
B

Posted: Sun Aug 14, 2005 10:37 am
by John Cartwright
what?

Posted: Sun Aug 14, 2005 10:44 am
by Addos
If I have in my MySql database a text paragraph containing a web address such as this http://www.me.com but this will only display as a line of text when it shows up in the browser. I know if I was doing this manually I’d be adding <a href="http://etc</a> to the link but I’m trying to find out how to have a script run on a page and search for hyper links and automatically add them where necessary. It’s exactly like when typing an email or in Word where a link is automatically recognised.
Thanks again
B

Posted: Sun Aug 14, 2005 11:16 am
by John Cartwright

Code: Select all

function urlhighlight($str) {
   preg_match_all("/http:\/\/?[^ ][^<]+/i",$str,$lnk);
   $size = sizeof($lnk[0]);
   $i = 0;
   while ($i < $size) {
       $len = strlen($lnk[0][$i]);
       if($len > 30) {
           $lnk_txt = substr($lnk[0][$i],0,30)."...";
       } else {
           $lnk_txt = $lnk[0][$i];    
       }
       $ahref = $lnk[0][$i];
       $str = str_replace($ahref,"<a href='$ahref' target='_blank'>$lnk_txt</a>",$str);
       $i++;
   }
   return $str;
}

Posted: Mon Aug 15, 2005 12:00 am
by Mastermind
SOmething wrong with your MYSQL Declaration

Posted: Mon Aug 15, 2005 12:40 am
by John Cartwright
Mastermind wrote:SOmething wrong with your MYSQL Declaration
wha? :?

Posted: Mon Aug 15, 2005 1:33 am
by s.dot
PHPBB's linking function

Code: Select all

$data = "a string with a link - http://www.domain.com";

function make_clickable($text) 
{ 
   $ret = ' ' . $text; 
   $ret = preg_replace("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);  
   $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret); 
   $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret); 
   $ret = substr($ret, 1); 
   return($ret); 
}

$newdata = make_clickable($data);
echo $newdata;