src/Path.php
public static function getRoot($path)
    {
        if ('' === $path) {
            return '';
        }

        Assert::string($path, 'The path must be a string. Got: %s');

        // Maintain scheme
        if (false !== ($pos = strpos($path, '://'))) {
            $scheme = substr($path, 0, $pos + 3);
            $path = substr($path, $pos + 3);
        } else {
            $scheme = '';
        }

        // UNIX root "/" or "\" (Windows style)
        if ('/' === $path[0] || '\\' === $path[0]) {
            return $scheme.'/';
        }

        $length = strlen($path);

        // Windows root
        if ($length > 1 && ctype_alpha($path[0]) && ':' === $path[1]) {
            // Special case: "C:"
            if (2 === $length) {
                return $scheme.$path.'/';
            }

            // Normal case: "C:/ or "C:\"
            if ('/' === $path[2] || '\\' === $path[2]) {
                return $scheme.$path[0].$path[1].'/';
            }
        }

        return '';
    }
public static function getRoot($path)
    {
        if ('' === $path) {
            return '';
        }

        Assert::string($path, 'The path must be a string. Got: %s');

        // Maintain scheme
        if (false !== ($pos = strpos($path, '://'))) {
            $scheme = substr($path, 0, $pos + 3);
            $path = substr($path, $pos + 3);
        } else {
            $scheme = '';
        }

        // UNIX root "/" or "\" (Windows style)
        if ('/' === $path[0] || '\\' === $path[0]) {
            return $scheme.'/';
        }

        $length = strlen($path);

        // Windows root
        if ($length > 1 && ctype_alpha($path[0]) && ':' === $path[1]) {
            // Special case: "C:"
            if (2 === $length) {
                return $scheme.$path.'/';
            }

            // Normal case: "C:/ or "C:\"
            if ('/' === $path[2] || '\\' === $path[2]) {
                return $scheme.$path[0].$path[1].'/';
            }
        }

        return '';
    }
public static function getRoot ( $ path ) { if ( '' === $ path ) { return '' ; } Assert :: string ( $ path , 'The path must be a string. Got: %s' ) ; // Maintain scheme if ( false !== ( $ pos = strpos ( $ path , '://' ) ) ) { $ scheme = substr ( $ path , 0 , $ pos + 3 ) ; $ path = substr ( $ path , $ pos + 3 ) ; } else { $ scheme = '' ; } // UNIX root "/" or "\" (Windows style) if ( '/' === $ path [ 0 ] || '\\' === $ path [ 0 ] ) { return $ scheme . '/' ; } $ length = strlen ( $ path ) ; // Windows root if ( $ length > 1 && ctype_alpha ( $ path [ 0 ] ) && ':' === $ path [ 1 ] ) { // Special case: "C:" if ( 2 === $ length ) { return $ scheme . $ path . '/' ; } // Normal case: "C:/ or "C:\" if ( '/' === $ path [ 2 ] || '\\' === $ path [ 2 ] ) { return $ scheme . $ path [ 0 ] . $ path [ 1 ] . '/' ; } } return '' ; }
Returns the root directory of a path.

The result is a canonical path.

@param string $path A path string.

@return string The canonical root directory. Returns an empty string if
the given path is relative or empty.

@since 1.0 Added method.
@since 2.0 Method now fails if $path is not a string.
Returns the root directory of a path .