note 86115 deleted from function.parse-url by cmb
| From: | cmb@php.net | Date: | Sun, 16 Jun 2019 08:24:17 +0000 |
| Subject: | note 86115 deleted from function.parse-url by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211283@lists.php.net to get a copy of this message | ||
Note Submitter: FredLudd at gmail dot com
----
Another shot at trying to find a better parser. I noticed that the laulibrius/theoriginalmarksimpson
functions didn't quite handle the URL for the page they were displayed on. For my mirror, ca3,
this is
http://ca3.php.net/manual/en/function.parse-url.php
Run it through the function and it parses to
scheme => http
login =>
pass =>
host => ca3.php.net
ip =>
subdomain => ca3
domain => php.
extension => net
port =>
path => /manual/en/function.parse
file => function.parse
that is, the file name gets a bit mangled
Rather than tweak the function's regular expression yet again, I opted to adapt a RegExp that
served me well in Javascript:
function j_parseUrl($url) {
$r = "(?:([a-z0-9+-._]+)://)?";
$r .= "(?:";
$r .= "(?:((?:[a-z0-9-._~!$&'()*+,;=:]|%[0-9a-f]{2})*)@)?";
$r .= "((?:[a-z0-9-._~!$&'()*+,;=]|%[0-9a-f]{2})*)";
$r .= "(?::(\d*))?";
$r .= "(/(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9a-f]{2})*)?";
$r .= "|";
$r .= "(/?";
$r .= "(?:[a-z0-9-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+";
$r .= "(?:[a-z0-9-._~!$&'()*+,;=:@\/]|%[0-9a-f]{2})*";
$r .= ")?";
$r .= ")";
$r .= "(?:\?((?:[a-z0-9-._~!$&'()*+,;=:\/?@]|%[0-9a-f]{2})*))?";
$r .= "(?:#((?:[a-z0-9-._~!$&'()*+,;=:\/?@]|%[0-9a-f]{2})*))?";
preg_match("
$ri", $url, $match);
$parts = array(
"scheme"=>'',
"userinfo"=>'',
"authority"=>'',
"host"=> '',
"port"=>'',
"path"=>'',
"query"=>'',
"fragment"=>'');
switch (count ($match)) {
case 9: $parts['fragment'] = $match[8];
case 8: $parts['query'] = $match[7];
case 7: $parts['path'] = $match[6];
case 6: $parts['path'] = $match[5] . $parts['path'];
case 5: $parts['port'] = $match[4];
case 4: $parts['host'] = $match[3];
case 3: $parts['userinfo'] = $match[2];
case 2: $parts['scheme'] = $match[1];
}
$parts['authority'] =
($parts['userinfo']?$parts['userinfo']."@":"").
$parts['host'].
($parts['port']?":".$parts['port']:"");
return $parts;
}
This function, when fed "http://ca3.php.net/manual/en/function.parse-url.php",
returns
scheme => http
userinfo =>
authority => ca3.php.net
host => ca3.php.net
port =>
path => /manual/en/function.parse-url.php
query =>
fragment =>
which is somewhat closer to my needs.
But everything should be tested against the two examples provided by RFC3986,
/* line too long for this site's commnet handler */
"foo://username:password@example.com:8042".
"/over/there/index.dtb;type=animal?name=ferret#nose"
and
"urn:example:animal:ferret:nose"
Here the native function parse_url() performs admirably on that "urn:" example. Mine fails
to pick out the path ("example:animal:ferret:nose") and the
laulibrius/theoriginalmarksimpson function can't decipher anything there. On the
"foo:" example, both my function and parse_url() get it right, while the other examples on
this page don't.
The laulibrius/theoriginalmarksimpson function delivers
scheme => foo
login => username
pass => password
host => example.com
ip =>
subdomain =>
domain => example.
extension => com
port => 8042
path => /over/there/index.dtb
file => index.dtb
As you can see, the query string ("name=ferret") and fragment ("nose") have
dropped off, as well as the parameter ("type=animal").