Relative + base URL to absolute URL?
<p>Basically, given a base URL like</p>

<pre><code>file:///path/to/some/file.html
</code></pre>

<p>And a relative URL like</p>

<pre><code>another_file.php?id=5
</code></pre>

<p>I want to get out</p>

<pre><code>file:///path/to/some/another_file.php?id=5
</code></pre>

<p>I found <a href="http://publicmind.in/blog/urltoabsolute/" rel="nofollow">this script</a> (which is the same as <a href="http://nadeausoftware.com/articles/2008/05/php_tip_how_convert_relative_url_absolute_url#Downloads" rel="nofollow">this one</a>) but it doesn't seem to work on the <code>file://</code> scheme. I'm doing some local tests before I go live with my code, so I'd like to work on both <code>file://</code> and <code>http://</code>.</p>

<p>Any one know of a script/function that will do this?</p>

<p>In C#, I'd use <a href="http://msdn.microsoft.com/en-us/library/9hst1w91.aspx" rel="nofollow">Uri(Uri base, string rel)</a>.</p>

<hr>

<p>The above is just an example. It should work on <em>any</em> URL that you could throw into <code>&lt;a href="xxx"&gt;</code>.</p>

<hr>

<p>This is the best I've got so far, but it won't handle <code>..</code> and probably a few other things:</p>

<pre><code>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&gt;0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
    return $parse['scheme'].'://'.$abs;
}
</code></pre>
