Yes, the hiccup is in the preg_replace. What's happening is that the matched URL (www.example.com/?something=somevalue, for example) is being interpreted as a regexp - with all the special interpretations that characters like . and ? have (so that example will match things like www!example.com/something=somevalue and www example comsomething=somevalue.
Three solutions to this:
1) use preg_quote() (though you'll probably need to check the regexp delimiters;
2) use str_replace() so that the string is interpreted as an ordinary string and not as a regexp,
3) replace the whole thing from preg_match_all down with
$input = preg_replace('<(([A-Z]+://)|(www\\.))[A-Z0-9-.]+\\.\\S+>i', "<a href=\"$0\" target=\"_blank\">$0</a>", $input);