Looking for url validator for form input

Discussion in 'PHP' started by camp185, Nov 29, 2006.

  1. #1
    Hi,

    Anybody have a regular method to validate url when inputed through form?
     
    camp185, Nov 29, 2006 IP
  2. Fl1p

    Fl1p Active Member

    Messages:
    511
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #2
    What do you mean exactly by validating URL? You want to make sure the URL is in the right format like www.abc.com?
     
    Fl1p, Nov 29, 2006 IP
  3. camp185

    camp185 Well-Known Member

    Messages:
    1,653
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    180
    #3
    yes please.

    Is this enough, or do I need more...


    if(!preg_match("/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i",$urlsubmitted))
    {
    Echo"You must supply a valid URL.";
    Exit();
    }
     
    camp185, Nov 29, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    You can use parse_url() as well.

    
    
    function is_valid_url($url)
    {
         @extract(@parse_url($url));
    
         return isset($scheme, $host);
    }
    
    
    PHP:
    (Untested)

    Might not be THAT safe though.
     
    nico_swd, Nov 29, 2006 IP
  5. camp185

    camp185 Well-Known Member

    Messages:
    1,653
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    180
    #5
    Thanks...I was looking for a quick fix on the web, and just couldn't find one. That is why I cam here. I am actually going to use the preg method followed up by file type verification. So I think I answered my own question.
     
    camp185, Nov 29, 2006 IP