Basically, given a base URL like
file:///path/to/some/file.html
And a relative URL like
another_file.php?id=5
I want to get out
file:///path/to/some/another_file.php?id=5
I found this script (which is the same as this one) but it doesn't seem to work on the file://
scheme. I'm doing some local tests before I go live with my code, so I'd like to work on both file://
and http://
.
Any one know of a script/function that will do this?
In C#, I'd use Uri(Uri base, string rel).
The above is just an example. It should work on any URL that you could throw into <a href="xxx">
.
This is the best I've got so far, but it won't handle ..
and probably a few other things:
function rel2abs($base, $rel) {
if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
$parse = parse_url($base);
$path = preg_replace('#/[^/]*$#', '', $parse['path']);
if ($rel[0] == '/') $path = '';
$abs = (isset($path['host'])?$path['host']:'')."$path/$rel";
$re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
return $parse['scheme'].'://'.$abs;
}