function remove_dot_segments($input)
{
$output = '';
while (strpos($input, './') !== false || strpos($input, '/.') !== false || $input === '.' || $input === '..') {
// A: If the input buffer begins with a prefix of "../" or "./", then remove that prefix from the input buffer; otherwise,
if (strpos($input, '../') === 0) {
$input = substr($input, 3);
} elseif (strpos($input, './') === 0) {
$input = substr($input, 2);
} elseif (strpos($input, '/./') === 0) {
$input = substr_replace($input, '/', 0, 3);
} elseif ($input === '/.') {
$input = '/';
} elseif (strpos($input, '/../') === 0) {
$input = substr_replace($input, '/', 0, 4);
$output = substr_replace($output, '', strrpos($output, '/'));
} elseif ($input === '/..') {
$input = '/';
$output = substr_replace($output, '', strrpos($output, '/'));
} elseif ($input === '.' || $input === '..') {
$input = '';
} elseif (($pos = strpos($input, '/', 1)) !== false) {
$output .= substr($input, 0, $pos);
$input = substr_replace($input, '', 0, $pos);
} else {
$output .= $input;
$input = '';
}
}
return $output . $input;
}