GuzzleHttp\Psr7\UriResolver::removeDotSegments PHP Method

removeDotSegments() public static method

Removes dot segments from a path and returns the new path.
public static removeDotSegments ( string $path ) : string
$path string
return string
    public static function removeDotSegments($path)
    {
        if ($path === '' || $path === '/') {
            return $path;
        }
        $results = [];
        $segments = explode('/', $path);
        foreach ($segments as $segment) {
            if ($segment === '..') {
                array_pop($results);
            } elseif ($segment !== '.') {
                $results[] = $segment;
            }
        }
        $newPath = implode('/', $results);
        if ($path[0] === '/' && (!isset($newPath[0]) || $newPath[0] !== '/')) {
            // Re-add the leading slash if necessary for cases like "/.."
            $newPath = '/' . $newPath;
        } elseif ($newPath !== '' && ($segment === '.' || $segment === '..')) {
            // Add the trailing slash if necessary
            // If newPath is not empty, then $segment must be set and is the last segment from the foreach
            $newPath .= '/';
        }
        return $newPath;
    }

Usage Example

 /**
  * Returns a normalized URI.
  *
  * The scheme and host component are already normalized to lowercase per PSR-7 UriInterface.
  * This methods adds additional normalizations that can be configured with the $flags parameter.
  *
  * PSR-7 UriInterface cannot distinguish between an empty component and a missing component as
  * getQuery(), getFragment() etc. always return a string. This means the URIs "/?#" and "/" are
  * treated equivalent which is not necessarily true according to RFC 3986. But that difference
  * is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well.
  *
  * @param UriInterface $uri   The URI to normalize
  * @param int          $flags A bitmask of normalizations to apply, see constants
  *
  * @return UriInterface The normalized URI
  * @link https://tools.ietf.org/html/rfc3986#section-6.2
  */
 public static function normalize(UriInterface $uri, $flags = self::PRESERVING_NORMALIZATIONS)
 {
     if ($flags & self::CAPITALIZE_PERCENT_ENCODING) {
         $uri = self::capitalizePercentEncoding($uri);
     }
     if ($flags & self::DECODE_UNRESERVED_CHARACTERS) {
         $uri = self::decodeUnreservedCharacters($uri);
     }
     if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' && ($uri->getScheme() === 'http' || $uri->getScheme() === 'https')) {
         $uri = $uri->withPath('/');
     }
     if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') {
         $uri = $uri->withHost('');
     }
     if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && Uri::isDefaultPort($uri)) {
         $uri = $uri->withPort(null);
     }
     if ($flags & self::REMOVE_DOT_SEGMENTS && !Uri::isRelativePathReference($uri)) {
         $uri = $uri->withPath(UriResolver::removeDotSegments($uri->getPath()));
     }
     if ($flags & self::REMOVE_DUPLICATE_SLASHES) {
         $uri = $uri->withPath(preg_replace('#//++#', '/', $uri->getPath()));
     }
     if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') {
         $queryKeyValues = explode('&', $uri->getQuery());
         sort($queryKeyValues);
         $uri = $uri->withQuery(implode('&', $queryKeyValues));
     }
     return $uri;
 }
All Usage Examples Of GuzzleHttp\Psr7\UriResolver::removeDotSegments