I'm trying to create a link within some text that is displayed from a database table. I want it to be able to scan the text and convert anything that has a 'www.' or 'http://' to a link.
Here's the info.
<p><?php echo nl2br($row_recent_event['description']); ?></p>
I was advised to use the following regular expression:
<?php
function hyperlink($text) {
// match protocol://address/path/file.extension?some=variable&another=asf%
$text = preg_replace("/\s([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*
[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/i",
" <a href=\"$1\" target=\"_blank\">$1</a>$2", $text);
// match www.something.domain/path/file.extension?some=variable&another=asf%
$text = preg_replace("/\s(www\.[a-z][a-z0-9\_\.\-]*
[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/i",
" <a href=\"http://$1\" target=\"_blank\">$1</a>$2", $text);
// match name@address
$text = preg_replace("/\s([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*
\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})([\s|\.|\,])/i",
" <a href=\"mailto://$1\">$1</a>$2", $text);
return $text;
}
?>
Question is, how do I get it to work? For example, within the database table there is some text in a field that has the text 'www.yadayadayada.com'. I would like to for this function to recognize the text and create the link.
Any suggestions?
toad78