note 90365 added to function.parse-url
| From: | theoriginalmarksimpsonatgmaildotcom at osu1 dot php dot net | Date: | Sat, 18 Apr 2009 00:08:49 +0000 |
| Subject: | note 90365 added to function.parse-url | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153458@lists.php.net to get a copy of this message | ||
An update to the function by FredLudd at gmail dot com. I added IPv6 functionality as well.
<?php
function j_parseUrl($url) {
$r = "(?:([a-z0-9+-._]+)://)?";
$r .= "(?:";
$r .= "(?:((?:[a-z0-9-._~!$&'()*+,;=:]|%[0-9a-f]{2})*)@)?";
$r .= "(?:\[((?:[a-z0-9:])*)\])?";
$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 10: $parts['fragment'] = $match[9];
case 9: $parts['query'] = $match[8];
case 8: $parts['path'] = $match[7];
case 7: $parts['path'] = $match[6] . $parts['path'];
case 6: $parts['port'] = $match[5];
case 5: $parts['host'] = $match[3]?"[".$match[3]."]":$match[4];
case 4: $parts['userinfo'] = $match[2];
case 3: $parts['scheme'] = $match[1];
}
$parts['authority'] =
($parts['userinfo']?$parts['userinfo']."@":"").
$parts['host'].
($parts['port']?":".$parts['port']:"");
return $parts;
}
?>
When using the url
/* line too long for this site's comment handler */
"foo://username:password@[2001:4860:0:2001::68]:8042".
"/over/there/index.dtb;type=animal?name=ferret#nose"
The original would return
Array
(
[scheme] => foo
[userinfo] => username:password
[authority] => username:password@
[host] =>
[port] =>
[path] =>
[query] =>
[fragment] =>
)
The new one returns
Array
(
[scheme] => foo
[userinfo] => username:password
[authority] => username:password@[2001:4860:0:2001::68]:8042
[host] => [2001:4860:0:2001::68]
[port] => 8042
[path] => /over/there/index.dtb;type=animal
[query] => name=ferret
[fragment] => nose
)
All of the other examples FredLudd used below still work exactly the same.
----
Server IP: 72.55.140.6
Probable Submitter: 65.92.44.23
----
Manual Page -- http://www.php.net/manual/en/function.parse-url.php
Edit -- https://master.php.net/note/edit/90365
Del: integrated -- https://master.php.net/note/delete/90365/integrated
Del: useless -- https://master.php.net/note/delete/90365/useless
Del: bad code -- https://master.php.net/note/delete/90365/bad+code
Del: spam -- https://master.php.net/note/delete/90365/spam
Del: non-english -- https://master.php.net/note/delete/90365/non-english
Del: in docs -- https://master.php.net/note/delete/90365/in+docs
Del: other reasons-- https://master.php.net/note/delete/90365
Reject -- https://master.php.net/note/reject/90365
Search -- https://master.php.net/manage/user-notes.php