Convert URLs in String to Hyperlinks

I’m attempting to convert the URLs in a string into clickable hyperlinks. I wrote a little function but it doesn’t seem to be doing the trick:

function parseHyperlinks($string) {
    // Add <a> tags around all hyperlinks in $string
    return ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\\"\\\\0\\" target=\\"_blank\\">\\\\0</a>", $string);
}

Now before you tell me that ereg_replace is deprecated - I know it is. This is an old function I wrote some time ago and just archived it. I’m using PHP 5.2.9 which technically means it should work. I’m happy to accept a preg_replace function but not sure what the differences are.

When I use this function, no errors are returned but no results are returned either. I’m sure there’s a technical term for this but the PHP stops processing and just spits out what it already came up with. No additional HTML is sent to the browser after this bit of code - it’s blank.

Any help is appreciated. I’m not against abandoning this function completely in favor of a different solution you recommend. :slight_smile:

If execution stops, an error likely occurred. Anything in the PHP error log or Apache error log?

Thanks Dan. I’m almost too embarrased to post it here, but this was the error:

[25-Feb-2010 18:15:30] PHP Fatal error: Call to undefined function parseHyperlinks() in /home/webadmin/stuho.us/html/include/twitter-feed.php on line 10

To my utter amazement, as soon as I actually included my functions.php file (with the user defined function) in the page it worked like a charm! Sometimes I question how I managed to even get dressed in the morning. :rolleyes:

Do you think I should still update this function to use preg_replace?

I wrote a function a while back that could handle almost any format that a link might come under…also set a maximum length.