ConFoo Montreal 2026: Call for Papers

Voting

: min(three, one)?
(Example: nine)

The Note You're Voting On

David Beck
18 years ago
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

?>

<< Back to user notes page

To Top