Need a URL regex (URL verification)

Discussion in 'PHP' started by MCJim, Aug 23, 2008.

  1. #1
    I need a complete URL regex. In other words, I need a script that verifies whether or not a URL is valid.

    Ex. http://www.domain.com is valid
    http://www.$#.com is not valid

    Any help is appreciated. I googled it but have had no luck so far... the ones I found were incomplete or not working. Thanks!
     
    MCJim, Aug 23, 2008 IP
  2. Freewebspace

    Freewebspace Notable Member

    Messages:
    6,213
    Likes Received:
    370
    Best Answers:
    0
    Trophy Points:
    275
    #2
    Simply way would be to check whether the site is online or not !

    All the invalid urls would not be online,so you can use this code

    
    $url="http://www.google.com";
    $url1=@file_get_contents($url);
    if(!($url1))
    {
    echo"Invalid url";
    }
    {
    echo"Valid url";
    }
    PHP:
     
    Freewebspace, Aug 24, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Don't use file_get_contents(). It's slow and takes a lot of memory. If you want to go this way at all, use get_headers().
     
    nico_swd, Aug 24, 2008 IP
  4. ash84

    ash84 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    is there anyway to do this using regex? i need this too validate links aswell
     
    ash84, Aug 24, 2008 IP
  5. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi,

    Theres plenty of different email checking methods on google - Quick search came up with the following explaining email validation (using regular expressions)

    www.addedbytes.com/php/email-address-validation

    Hope it helps,

    Regards,

    Steve
     
    Steve136, Aug 24, 2008 IP
  6. beacon

    beacon Peon

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This simple regexp:
    
    $pat = '~[url]http://www\[/url].[a-z0-9_-]+\.[a-z\.]{2,5}~i';
    $url = 'http://www.domain.com';
    if(preg_match($pat,$url)) {
      ..........
      ..valid url.....
      }else{
      ... not valid .....
     }
    Code (markup):
     
    beacon, Aug 24, 2008 IP
  7. ash84

    ash84 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    how would i remove the www. from it? as not all urls may contain the www subdomain
     
    ash84, Aug 24, 2008 IP
  8. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #8
    use
    $url = "www.google.com";
    $newurl = preg_repace('/www\./'.'//',$url);
    echo $newurl;


    something like that
     
    cornetofreak, Aug 24, 2008 IP
  9. beacon

    beacon Peon

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Try other pattern:
    $pat = '~https?://[a-z0-9_-\.]+\.[a-z\.]{2,5}[\s/]?~i'';
     
    beacon, Aug 24, 2008 IP
  10. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #10
    EricBruggema, Aug 25, 2008 IP
  11. beacon

    beacon Peon

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    beacon, Aug 25, 2008 IP
  12. grandpa

    grandpa Active Member

    Messages:
    185
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #12
    grandpa, Aug 25, 2008 IP