Right and left trim route from blanks and undesired characters.

@param $path

@return string
@throws \Exception
546636
public function trimPath($path) {
        // Rewrite leading double slashes.
        $path = preg_replace("/^\/\//", "/", $path);

        // If there are more double slashes route is invalid.
        if (preg_match("/\/{2,}/", $path)) {
            throw new \Exception('Route path has invalid format ' . $path);
        }

        // Trim chars.
        return trim($path, " \t\n\r\0\x0B/");
    }
1294254
public function stripScheme($path)
    {
        $scheme = preg_split('#://#', $path, 2);
        $scheme = end($scheme);

        return '/'.ltrim($scheme, '/');
    }
191844
protected function removeDotSegments($path): string
    {
        if ($path === '' || $path === '/') {
            return $path;
        }

        $results = [];
        $segments = explode('/', $path);
        $segment = null;
        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;
    }
321431
public static function normalize(string $path): string {
		if (!(false === strpos($path, '://'))) {
			$ex = explode("://", $path);

			return $ex[0] . "://" . "/" . ltrim(Util::normalizePath("/" . ltrim($ex[1], "/")));
		}

		return "/" . ltrim(Util::normalizePath($path), "/");
	}
768264
protected static function normalizePath($path)
    {
        if ('/' === substr($path, 0, 1)) {
            $path = static::removePathDotSegments($path);
        }

        $path = static::encodePath(
            static::decodeUrlEncodedChars(
                $path,
                '/[' . static::CHAR_UNRESERVED . ':@&=\+\$,\/;%]/'
            )
        );

        if (strlen($path) > 1 && '/' === substr($path, -1)) {
            $path = substr($path, 0, -1);
        }

        return $path;
    }
1362823
protected function _normpath($path) {
		if (empty($path)) {
			$path = '.';
		}
		// path must be start with /
		$path = preg_replace('|^\.\/?|', '/', $path);
		$path = preg_replace('/^([^\/])/', "/$1", $path);

		if (strpos($path, '/') === 0) {
			$initial_slashes = true;
		} else {
			$initial_slashes = false;
		}
			
		if (($initial_slashes) 
		&& (strpos($path, '//') === 0) 
		&& (strpos($path, '///') === false)) {
			$initial_slashes = 2;
		}
			
		$initial_slashes = (int) $initial_slashes;

		$comps = explode('/', $path);
		$new_comps = array();
		foreach ($comps as $comp) {
			if (in_array($comp, array('', '.'))) {
				continue;
			}
				
			if (($comp != '..') 
			|| (!$initial_slashes && !$new_comps) 
			|| ($new_comps && (end($new_comps) == '..'))) {
				array_push($new_comps, $comp);
			} elseif ($new_comps) {
				array_pop($new_comps);
			}
		}
		$comps = $new_comps;
		$path = implode('/', $comps);
		if ($initial_slashes) {
			$path = str_repeat('/', $initial_slashes) . $path;
		}
		
		return $path ? $path : '.';
	}
1184481
public function path($path)
    {
        if ($path !== '/' . trim($path, '/') . '/') {
            throw new Exception('Invalid path "' . $path . '", use like this /foo/', 2);
        }

        $this->path = $path;

        return $this;
    }
155402
private function normalize(string $path): string
    {
        if ($path === '') {
            return '/';
        }

        if (strlen($path) > 1) {
            if ($this->trailingSlash) {
                if (substr($path, -1) !== '/' && !pathinfo($path, PATHINFO_EXTENSION)) {
                    return $path.'/';
                }
            } else {
                return rtrim($path, '/');
            }
        }

        return $path;
    }
702172
private function _filterPath($path)
    {
        $path = preg_replace_callback(
            '/(?:[^' . self::UNRESERVED_CHARACTERS . self::SUB_DELIMS_CHARACTERS . '%:@\/]+|%(?![A-Fa-f0-9]{2}))/',
            [$this, '_rawUrlEncode'],
            $path
        );

        if ($path && $path[0] === '/') {
            $path = '/' . ltrim($path, '/');
        }

        return $path;
    }
885525
public static function clean_up_path($path)
    {

        if (empty($path)) return '';
        if (strpos($path, '\\') !== false) {
            return str_replace("\\\\", "\\", $path);
        } else {
            return str_replace("//", "/", $path);
        }
    }
654948
static public function cleanPath($path, $ds = DIRECTORY_SEPARATOR)
	{
	    if (!is_string($path)) {
	        throw new \UnexpectedValueException(__CLASS__ . '::clean $path is not a string.');
	    }
	    
	    $path = trim($path);
	    
	    if (($ds == '\\') && ($path[0] == '\\' ) && ( $path[1] == '\\' ))
	    // Remove double slashes and backslashes and convert all slashes and backslashes to DIRECTORY_SEPARATOR
	    // If dealing with a UNC path don't forget to prepend the path with a backslash.
	    {
	        $path = "\\" . preg_replace('#[/\\\\]+#', $ds, $path);
	    }
	    else
	    {
	        $path = preg_replace('#[/\\\\]+#', $ds, $path);
	    }
	    
	    return $path;
	}
1160430
private function parsePath($path)
    {
        if (empty($path)) {
            $path = Map::DEFAULT_URL_DELIMITER;
        }

        // get the clean path
        return $this->urlParser->parse($path);
    }
1332184
protected function normalizePath($path)
    {
        if (preg_match("/^id:.*|^rev:.*|^(ns:[0-9]+(\/.*)?)/", $path) === 1) {
            return $path;
        }

        $path = (trim($path, '/') === '') ? '' : '/'.$path;

        return str_replace('//', '/', $path);
    }
1166055
private static function removeDotSegments(string $path)
    {
        $result = '';

        while (!\in_array($path, ['', '.', '..'], true)) {
            if ('.' === $path[0] && (0 === strpos($path, $p = '../') || 0 === strpos($path, $p = './'))) {
                $path = substr($path, \strlen($p));
            } elseif ('/.' === $path || 0 === strpos($path, '/./')) {
                $path = substr_replace($path, '/', 0, 3);
            } elseif ('/..' === $path || 0 === strpos($path, '/../')) {
                $i = strrpos($result, '/');
                $result = $i ? substr($result, 0, $i) : '';
                $path = substr_replace($path, '/', 0, 4);
            } else {
                $i = strpos($path, '/', 1) ?: \strlen($path);
                $result .= substr($path, 0, $i);
                $path = substr($path, $i);
            }
        }

        return $result;
    }
658302
public function applyPathPrefix($path)
    {
        $path = ltrim($path, '/');
        if (strlen($path) === 0) {
            return rtrim($this->getPathPrefix(), '/');
        }

        return $this->getPathPrefix() . $path;
    }
928176
protected function formatPath(string $path): string
    {
        $path = $this->filterPath($path)->withoutDotSegments();
        if ('' !== $this->base_uri->getAuthority() && '' !== $path->__toString()) {
            $path = $path->withLeadingSlash();
        }

        return (string) $path;
    }
1275111
public static function removeDotSegments($path)
    {
        static $noopPaths = ['' => true, '/' => true, '*' => true];
        static $ignoreSegments = ['.' => true, '..' => true];

        if (array_key_exists($path, $noopPaths)) {
            return $path;
        }

        $results = [];
        $segments = explode('/', $path);
        foreach ($segments as $segment) {
            if ($segment === '..') {
                array_pop($results);
            } elseif (!array_key_exists($segment, $ignoreSegments)) {
                $results[] = $segment;
            }
        }

        $newPath = implode('/', $results);
        // add the leading slash if necessary
        if (strpos($path, '/') === 0 && strpos($newPath, '/') !== 0) {
            $newPath = '/' . $newPath;
        }

        // add the trailing slash if necessary
        if ($newPath !== '/' && array_key_exists(end($segments), $ignoreSegments)) {
            $newPath .= '/';
        }

        return $newPath;
    }