I've created a message system and would like to auto-convert url-links in a message to clickable HTML links if a new message is posted. I wrote this simple function but it isn't working as it should:
// LINK ALL URLS
$message = ereg_replace("http://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $message);
$message = ereg_replace("(^| |\n)(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $message);
For some urls it is working, but with other urls there are problems and the results are like this:
<a href="http://www.example.com/index.php">http://www.example.com/index.php</a>?mode=index&page=1
or
<a href="http://www.youtube.com/watch">http://www.youtube.com/watch</a>?v=jSh5Y7jq9FQ
As you can see, it is not correctly converted inclusive the part behind the question mark. Can someone please fix / update my code above? And by the way, would there be perhaps another (and better!) solution instead of using *ereg_replace* ?