Hi,

i am making comment script in php and want to know how to check whether user submitted homepage url is valid.

i dont want nothing complex, just check if it starts with http:// and dont contain symbols that cant be used in url

cant you give me code or link?
and where i can read what symbols cant be used in url?

    how about this:

    if(ereg("^[http://]{1}([^[:space:]]*)[:alnum:]+[(\.){1}[:alnum:]]+(\/)[[:alnum]#?/&=]$" , $url)){
        //url valid
    }else{
        //url not valid
    }

    found this here: http://p2p.wrox.com/topic.asp?TOPIC_ID=2595

    a google search for url validation will bring up some variations

    edit: laserlight: your too fast for me!

      i tried it, but in my case it doesnt work. i need it also accept links with variables after /,

      like - http://abc.com/?id=12&cat=2#12

      your mentioned script says,
      NO!, http://abc.com/?id=12&cat=2#12 is NOT a valid url

      how to improve that script?

      function is_valid_url($url) {
        $pattern = "#^(http:\/\/|https:\/\/|www\.)(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?(\/)*$#i";
        if (!preg_match($pattern, $url)) {
          return false;
        } else {
          return true;
        }
      } // is_valid_url()
      
      $str = '//example.com';
      if (is_valid_url($str)) {
        echo 'YES, ' . $str . ' is a valid url';
      } else {
        echo 'NO!, ' . $str . ' is NOT a valid url';
      }
      
        Write a Reply...