Here's a function to canonicalize a URL containing relative paths. Ran into the problem when pulling links from a remote page.
<?php
function canonicalize($address)
{
$address = explode('/', $address);
$keys = array_keys($address, '..');
foreach($keys AS $keypos => $key)
{
array_splice($address, $key - ($keypos * 2 + 1), 2);
}
$address = implode('/', $address);
$address = str_replace('./', '', $address);
}
$url = 'http://www.example.com/something/../else';
echo canonicalize($url); //http://www.example.com/else
?>