views:

57

answers:

2

Does anyone know an up to date regular expression for validating URLs? I found a few on Google but they all allowed junk URL's i.e (www.google_com) when testing.

My regular expression knowledge is not so vast, so I would hate to put something together that would fail under pressure.

Thanks.

+1  A: 

You can use the filter functions in PHP

$filtered = filter_var($url, FILTER_VALIDATE_URL);

http://uk3.php.net/manual/en/function.filter-var.php

infinity
+1, no idea why this was voted down.
Wrikken
+1  A: 

Not every problem should be answered with a regex.

http://php.net/manual/en/function.parse-url.php

Andy Lester
Hm… Cite: "This function is ***not*** meant to validate the given URL, it only breaks it up into the above listed parts." ;-)
Tomalak
This answer looks like a clone from a thread I read.
Jamie Redmond
I haven't tested, but I think by "validate" they mean "check that there's an actual page there". If it's not a valid URL, I'd imagine the splitting algorithm would choke.
ceejayoz
@ceejayoz: That's of course nonsense. An URL can be valid without pointing to an existing resource. The function will not make an HTTP.
Tomalak