I want to search some text for a URL. If a URL is found, it should be turned into a HTML hyperlink. The text may also include an embedded youtube movie, so I included "[^\'"\w]" at the start of my search pattern.

I'm sure that there are much better and faster ways of achiving this task. Any help would be much appreciated.

PHP Code:
    //text to change
    
$textbody "This is some text with http://www.website.com/ a hyperlink included.";
    
    
//pattern for URL
    
$pattern '|[^\'"\w]+http://[\w.-/]+|';
    
    
//find URL in text
    
preg_match ($pattern$textbody$matches);
    
    
//put URL in
    
$replacement '<a rel="nofollow" href="' $matches[0] . '" target="_blank">' $matches[0] .'</a>';
    
    
//done
    
echo str_replace($matches[0], $replacement$textbody); 
Would this task be easier if I made sure every URL was wrapped in square brackets as shown below? I'm just learning Regular Expression, so I'm sure this can be implemented in a much better way.
PHP Code:
$textbody "This is some text with [url]http://www.website.com/[/url] a hyperlink included.";
    
    
$pattern1 '|\[url\]+http://[\w.-/]+\[/url\]|';
    
    
preg_match ($pattern1$textbody$matches);
    
    
$pattern '|http://[\w.-/]+|';
    
    
preg_match ($pattern$matches[0], $matches2);
    
    
$replacement '<a rel="nofollow" href="' $matches2[0] . '" target="_blank">' $matches2[0] .'</a>';
    
    echo 
preg_replace($pattern1$replacement$textbody); 
Thanks for reading!