if (!preg_match("#^http://www\.[a-z0-9-_.]+\.[a-z]{2,4}$#i",$url)) {
echo "wrong url";
} else {
echo "ok";
}
that looks good, except that only letters, numbers, and hypens are allowed in a domain name (underscores are not allowed), and due to ICANN limitation domain names are limited to 64 characters. This regular expression also limits a lot of cases, for instance none of the following special cases would work...
any website with a subdomain/or lack of one other than www, for instance
http://slashdot.org/ (has no www and no subdomain)
http://en.wikipedia.org/ (has a subdomain en, but no www)
subfolders
http://www.mozilla.com/en-US/firefox/
ip addresses...
http://64.233.167.99/
you will actually get google.com (that is its ip address as of this current moment).
specifying port numbers:
http://glcfapp.umiacs.umd.edu:8080
entering username/passwords directly into the url
http://username: password@example.com
i recommend going here: http://regexlib.com/Search.aspx
and performing a search for category "Uri" and see which regular expression you thing would work best for this example. here is a sample i got from that site, of course this one is not perfect either, but it's another sample.
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?
just know that it will probably be virtually impossible to find a perfect regex expression to match all possible URL's, one way would be to see if it starts with http:// and if it does, then see if you can make a connection to it with php, if the headers return a status code of 200, then you are good and you know it's a valid url, but that is a lot of work in itself.