This was a fun problem to solve. 🙂
I googled and found this wonderful link: http://www.desilva.biz/php/xtracturl.html
It gives you the URL pattern to use. Then I used the function preg_match() to find the URL in a string.
Then I wrote some sample code that may help you:
<?php
$myText = 'I love to surf on [url]http://phpbuilder.com[/url]';
$urlpattern = '/((http|https|ftp):\/\/|www)' // line 1
.'[a-z0-9\-\._]+\/?[a-z0-9_\.\-\?\+\/~=&#;,]*' // line 2
.'[a-z0-9\/]{1}/si'; // line 3
$match = preg_match($urlpattern, $myText, $matches);
if($match)
{
echo 'The original string is:<br />'
. $myText . '<br /><br />';
echo 'The URL in the string is:<br />'
. $matches[0] . '<br /><br />';
$newText = eregi_replace ($matches[0], '<a href="' . $matches[0] . '">' . $matches[0] .
'</a>', $myText);
echo 'The new string is:<br />' . $newText
. '<br /><br />';
}
else
{
echo 'There are no URLs in the String.';
}
?>
the variable $urlpattern is explained in the link above.
$myText = 'I love to surf on http://phpbuilder.com';
This is the string I used. You can replace this with your textarea content.
$match = preg_match($urlpattern, $myText, $matches);
This return 1 if there is a matching pattern in the string.
$matches is an array that returns the string that matches. $match[0] returns the full URL.
If you have any other questions on the script, just leave a post. 🙂 It should get you started on your script.