How do I linkify urls in a string with php?
lib_autolink
does a pretty good job, avoiding pitfalls like extra punctuation after the link and links inside HTML tags:
https://github.com/iamcal/lib_autolink
You can use the following:
$string = "Look on http://www.google.com";
$string = preg_replace(
"~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~",
"<a href=\"\\0\">\\0</a>",
$string);
PHP versions < 5.3 (ereg_replace) otherwise (preg_replace)
Have a look at regular expressions. You would then do something like:
$text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);