Re: validate URL

From: Date: Thu, 04 Dec 2003 16:28:46 +0000
Subject: Re: validate URL
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-24137@lists.php.net to get a copy of this message
Jeroen Houben wrote:
Hi, I saw this code in the Validate package (in CVS):
       if (preg_match('/^(https?|ftp)$/i', @$purl['scheme']) && !empty($purl['host'])) {
I'm no expert on regex but does this mean only http://* and ftp://* is valid? Jeroen
From the URI RFC, comes this amazing section: B. Parsing a URI Reference with a Regular Expression As described in Section 4.3, the generic URI syntax is not sufficient to disambiguate the components of some forms of URI. Since the "greedy algorithm" described in that section is identical to the disambiguation method used by POSIX regular expressions, it is natural and commonplace to use a regular expression for parsing the potential four components and fragment identifier of a URI reference. The following line is the regular expression for breaking-down a URI reference into its components.
      ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
       12            3  4          5       6  7        8 9
The numbers in the second line above are only to assist readability; they indicate the reference points for each subexpression (i.e., each paired parenthesis). We refer to the value matched for subexpression <n> as $<n>. For example, matching the above expression to
      http://www.ics.uci.edu/pub/ietf/uri/#Related
results in the following subexpression matches:
      $1 = http:
      $2 = http
      $3 = //www.ics.uci.edu
      $4 = www.ics.uci.edu
      $5 = /pub/ietf/uri/
      $6 = <undefined>
      $7 = <undefined>
      $8 = #Related
      $9 = Related
where <undefined> indicates that the component is not present, as is the case for the query component in the above example. Therefore, we can determine the value of the four components and fragment as
      scheme    = $2
      authority = $4
      path      = $5
      query     = $7
      fragment  = $9
I have yet to actually get this regex to work with ereg... I think unless you need to *parse* the URI a lot of the parens could be removed... and moving it to PCRE instead of POSIX would be beneficial. This needs a regex guru for sure :D - Davey

« previous php.pear.dev (#24137) next »