Active Link via database query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Active Link via database query

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what?
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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;
}
User avatar
Mastermind
Forum Commoner
Posts: 36
Joined: Thu Mar 10, 2005 2:38 am
Location: CDO,Philippines
Contact:

Post by Mastermind »

SOmething wrong with your MYSQL Declaration
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Mastermind wrote:SOmething wrong with your MYSQL Declaration
wha? :?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply