How to convert URLs in string into Links

I want to convert URL in $string into link, that is convert the URL to <a href=””></a>. For achive this I have written following helpful PHP code.

Example :

$string = "This is my PHP blog";
$output = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\">$1</a>", $string);
echo $output;

// Output will display – This is my PHP blog <a href=””></a>