Page 1 of 1

[Solved] Making a hyper link active

Posted: Tue Dec 26, 2006 3:15 pm
by Addos
Is there any script that I can run in the head of a page that will automatically make any link in a text with http://www.thissite.com become an active link?

I have tried the script below with limited success as technically I need to run this through a loop and it’s telling me that it cannot redeclare make_clickable() previously declared si I’m puzzled as to what to do.

Code: Select all

<?PHP 
$data = $row_GetOrchestral['OrchDetails']; 
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); ?>
In essence this below is what I need to do and pick up any links such as http://www.link.com that are retrieved from the database.

Thanks
Brian

Code: Select all

<?php do { ?>
<strong><?php echo $row_GetOrchestral['OrchTitle']; ?></strong><br>
<?PHP 
$data = $row_GetOrchestral['OrchDetails']; 
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); 

?>  <?php echo nl2br($newdata); ?><br><br>
<?php } while ($row_GetOrchestral = mysql_fetch_assoc($GetOrchestral)); ?>

Posted: Tue Dec 26, 2006 3:17 pm
by John Cartwright
don't declare the function inside the loop, simply call it.

Posted: Tue Dec 26, 2006 3:29 pm
by Addos
Thanks for that but I did try that using the script below but what I get is a repeat of the <?php echo nl2br($newdata); ?> for example below returns...

Concerto for Guitar & Strings 1998
gui-solo, str
MS 20'
Availability - Contemporary Music Centre http://www.cmc.ie

Rajas 1998
gui-solo, str
MS 20'
Availability - Contemporary Music Centre http://www.cmc.ie

Tiento 1994
gui-solo, str
MS 20'
Availability - Contemporary Music Centre http://www.cmc.ie

And if you note that gui-solo, str MS 20' Availability - Contemporary Music Centre http://www.cmc.ie keeps repeating when this information should be different.
Thanks again
B

Code: Select all

<?PHP 
$data = $row_GetOrchestral['OrchDetails']; 
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); 

?>  
<?php do { ?>
<strong><?php echo $row_GetOrchestral['OrchTitle']; ?></strong><br>
<?php echo nl2br($newdata); ?><br><br>
<?php } while ($row_GetOrchestral = mysql_fetch_assoc($GetOrchestral)); ?>
</div>

Posted: Tue Dec 26, 2006 3:39 pm
by John Cartwright
Because you are not calling the function inside your loop.

Posted: Tue Dec 26, 2006 4:52 pm
by Addos
Thanks Jcart,
That sorted it and thanks for helping me.
B