SimplePie_Misc::remove_dot_segments PHP Method

remove_dot_segments() public method

public remove_dot_segments ( $input )
    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;
    }

Usage Example

Example #1
0
 function normalize_url($url)
 {
     $url = preg_replace_callback('/%([0-9A-Fa-f]{2})/', array('SimplePie_Misc', 'percent_encoding_normalization'), $url);
     $url = SimplePie_Misc::parse_url($url);
     $url['scheme'] = strtolower($url['scheme']);
     if ($url['authority'] !== '') {
         $url['authority'] = strtolower($url['authority']);
         $url['path'] = SimplePie_Misc::remove_dot_segments($url['path']);
     }
     return SimplePie_Misc::compress_parse_url($url['scheme'], $url['authority'], $url['path'], $url['query'], $url['fragment']);
 }