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
Active Link via database query
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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
Thanks again
B
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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;
}- Mastermind
- Forum Commoner
- Posts: 36
- Joined: Thu Mar 10, 2005 2:38 am
- Location: CDO,Philippines
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.