Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Path.php

function Path::getRoot

Returns the root directory of a path.

The result is a canonical path.

Return value

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

File

vendor/symfony/filesystem/Path.php, line 212

Class

Path
Contains utility methods for handling path strings.

Namespace

Symfony\Component\Filesystem

Code

public static function getRoot(string $path) : string {
    if ('' === $path) {
        return '';
    }
    // Maintain scheme
    if (false !== ($schemeSeparatorPosition = strpos($path, '://'))) {
        $scheme = substr($path, 0, $schemeSeparatorPosition + 3);
        $path = substr($path, $schemeSeparatorPosition + 3);
    }
    else {
        $scheme = '';
    }
    $firstCharacter = $path[0];
    // UNIX root "/" or "\" (Windows style)
    if ('/' === $firstCharacter || '\\' === $firstCharacter) {
        return $scheme . '/';
    }
    $length = \strlen($path);
    // Windows root
    if ($length > 1 && ':' === $path[1] && ctype_alpha($firstCharacter)) {
        // Special case: "C:"
        if (2 === $length) {
            return $scheme . $path . '/';
        }
        // Normal case: "C:/ or "C:\"
        if ('/' === $path[2] || '\\' === $path[2]) {
            return $scheme . $firstCharacter . $path[1] . '/';
        }
    }
    return '';
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal