View Full Version : Regex to check for a valid web address

08-31-2011, 04:05 AM
I need to codea regex to check if a user submitted a valid web address but ive no idea how to do it.

Id guess it needs to match [http(s)://][(a-zA-Z0-9 and - and .)][.(a-z){2+}]

but i know that wouldnt be the correct syntax for it, but i cant seem to get it right.

Can anyone help?

08-31-2011, 04:15 AM
Rather than writing a complex expression, check out filter_var():

$url = 'http://example.com';

$url_filtered = filter_var( $url, FILTER_VALIDATE_URL );

if ( FALSE !== $url_filtered )
// correct URL structure

08-31-2011, 04:45 AM
Ahh that looks better than a regex, thanks :)