Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    822
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Regex to check for a valid web address

    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?

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Rather than writing a complex expression, check out filter_var():
    http://www.php.net/filter_var
    PHP Code:
    $url 'http://example.com';

    $url_filtered filter_var$urlFILTER_VALIDATE_URL );

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


  • Users who have thanked kbluhm for this post:

    afdah (08-31-2011)

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    822
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Ahh that looks better than a regex, thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •