- array_splice: documentation ( source)
- implode: documentation ( source)
- str_replace: documentation ( source)
- explode: documentation ( source)
- array_keys: documentation ( source)
<?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);
return $address;
}
echo canonicalize("http://www.zakzak.co.jp/entertainment/ent-news/news/20140216//../../../../entertainment/ent-news/images/20140216/enn1402161257010-n1.jpg");