SimplePie_IRI::remove_dot_segments PHP Method

remove_dot_segments() public method

Remove dot segments from a path
public remove_dot_segments ( string $input ) : string
$input string
return string
    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

 /**
  * Create a new IRI object by resolving a relative IRI
  *
  * Returns false if $base is not absolute, otherwise an IRI.
  *
  * @param IRI|string $base (Absolute) Base IRI
  * @param IRI|string $relative Relative IRI
  * @return IRI|false
  */
 public static function absolutize($base, $relative)
 {
     if (!$relative instanceof SimplePie_IRI) {
         $relative = new SimplePie_IRI($relative);
     }
     if (!$relative->is_valid()) {
         return false;
     } elseif ($relative->scheme !== null) {
         return clone $relative;
     } else {
         if (!$base instanceof SimplePie_IRI) {
             $base = new SimplePie_IRI($base);
         }
         if ($base->scheme !== null && $base->is_valid()) {
             if ($relative->get_iri() !== '') {
                 if ($relative->iuserinfo !== null || $relative->ihost !== null || $relative->port !== null) {
                     $target = clone $relative;
                     $target->scheme = $base->scheme;
                 } else {
                     $target = new SimplePie_IRI();
                     $target->scheme = $base->scheme;
                     $target->iuserinfo = $base->iuserinfo;
                     $target->ihost = $base->ihost;
                     $target->port = $base->port;
                     if ($relative->ipath !== '') {
                         if ($relative->ipath[0] === '/') {
                             $target->ipath = $relative->ipath;
                         } elseif (($base->iuserinfo !== null || $base->ihost !== null || $base->port !== null) && $base->ipath === '') {
                             $target->ipath = '/' . $relative->ipath;
                         } elseif (($last_segment = strrpos($base->ipath, '/')) !== false) {
                             $target->ipath = substr($base->ipath, 0, $last_segment + 1) . $relative->ipath;
                         } else {
                             $target->ipath = $relative->ipath;
                         }
                         $target->ipath = $target->remove_dot_segments($target->ipath);
                         $target->iquery = $relative->iquery;
                     } else {
                         $target->ipath = $base->ipath;
                         if ($relative->iquery !== null) {
                             $target->iquery = $relative->iquery;
                         } elseif ($base->iquery !== null) {
                             $target->iquery = $base->iquery;
                         }
                     }
                     $target->ifragment = $relative->ifragment;
                 }
             } else {
                 $target = clone $base;
                 $target->ifragment = null;
             }
             $target->scheme_normalization();
             return $target;
         } else {
             return false;
         }
     }
 }