note 76979 added to function.parse-url
| From: | spam at paulisageek dot com | Date: | Wed, 08 Aug 2007 19:05:17 +0000 |
| Subject: | note 76979 added to function.parse-url | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129353@lists.php.net to get a copy of this message | ||
In reply to adrian,
Thank you very much for your function. There is a small issue with your relative protocol function.
You need to remove the // when making the url the path. Here is the new function.
function resolve_url($base, $url) {
if (!strlen($base)) return $url;
// Step 2
if (!strlen($url)) return $base;
// Step 3
if (preg_match('!^[a-z]+:!i', $url)) return $url;
$base = parse_url($base);
if ($url{0} == "#") {
// Step 2 (fragment)
$base['fragment'] = substr($url, 1);
return unparse_url($base);
}
unset($base['fragment']);
unset($base['query']);
if (substr($url, 0, 2) == "//") {
// Step 4
return unparse_url(array(
'scheme'=>$base['scheme'],
'path'=>substr($url,2),
));
} else if ($url{0} == "/") {
// Step 5
$base['path'] = $url;
} else {
// Step 6
$path = explode('/', $base['path']);
$url_path = explode('/', $url);
// Step 6a: drop file from base
array_pop($path);
// Step 6b, 6c, 6e: append url while removing "." and ".." from
// the directory portion
$end = array_pop($url_path);
foreach ($url_path as $segment) {
if ($segment == '.') {
// skip
} else if ($segment == '..' && $path &&
$path[sizeof($path)-1] != '..') {
array_pop($path);
} else {
$path[] = $segment;
}
}
// Step 6d, 6f: remove "." and ".." from file portion
if ($end == '.') {
$path[] = '';
} else if ($end == '..' && $path && $path[sizeof($path)-1]
!= '..') {
$path[sizeof($path)-1] = '';
} else {
$path[] = $end;
}
// Step 6h
$base['path'] = join('/', $path);
}
// Step 7
return unparse_url($base);
}
----
Server IP: 208.69.120.35
Probable Submitter: 207.126.230.225
----
Manual Page -- http://www.php.net/manual/en/function.parse-url.php
Edit -- https://master.php.net/note/edit/76979
Del: integrated -- https://master.php.net/note/delete/76979/integrated
Del: useless -- https://master.php.net/note/delete/76979/useless
Del: bad code -- https://master.php.net/note/delete/76979/bad+code
Del: spam -- https://master.php.net/note/delete/76979/spam
Del: non-english -- https://master.php.net/note/delete/76979/non-english
Del: in docs -- https://master.php.net/note/delete/76979/in+docs
Del: other reasons-- https://master.php.net/note/delete/76979
Reject -- https://master.php.net/note/reject/76979
Search -- https://master.php.net/manage/user-notes.php