Oihana PHP

getRoot.php

Table of Contents

Functions

getRoot()  : string
Extracts the root directory component of a given path.

Functions

getRoot()

Extracts the root directory component of a given path.

getRoot(string $path) : string

This function identifies the root portion of a file system path, including handling of protocol schemes. ( e.g., "file://", "s3://" )

UNIX root ("/"), and Windows root ( e.g., "C:/" ).

It returns the canonical root as a string, or an empty string if the path is relative or empty.

Behavior:

  • "file:///usr/bin""file:///"
  • "/usr/bin""/"
  • "C:\\Windows\\System32""C:/"
  • "relative/path""" (empty string)
Parameters
$path : string

The input path, optionally with a scheme.

Tags
example
use function oihana\files\getRoot;

echo getRoot("file:///var/log");        // "file:///"
echo getRoot("/usr/local/bin");         // "/"
echo getRoot("C:\\Windows\\System32");  // "C:/"
echo getRoot("D:");                     // "D:/"
echo getRoot("some/relative/path");     // ""
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The root component of the path, or an empty string if no root can be determined.


        
On this page

Search results