Oihana PHP

getSchemeAndHierarchy.php

Table of Contents

Functions

getSchemeAndHierarchy()  : array{0: ?string, 1: string}
Split a filename or URI into its scheme (if any) and hierarchical part.

Functions

getSchemeAndHierarchy()

Split a filename or URI into its scheme (if any) and hierarchical part.

getSchemeAndHierarchy(string $filename) : array{0: ?string, 1: string}

Logic

  • Detect the first “://” only once – no array allocation if not present.
  • Accept schemes that match RFC‑3986 [A‑Za‑z][A‑Za‑z0‑9+\-.]*.
  • Return [$scheme, $hierarchy], where $scheme is null when absent.
Parameters
$filename : string

A path or URI such as file:///tmp/app.log or /etc/hosts.

Tags
throws
InvalidArgumentException

if the scheme is malformed (e.g. '1http://')

example
getSchemeAndHierarchy('s3://bucket/folder/img');    // ['s3',   'bucket/folder/img']
getSchemeAndHierarchy('/home/user/report.pdf');     // [null,  '/home/user/report.pdf']
getSchemeAndHierarchy('C:\\Windows\\notepad.exe');  // [null,  'C:\\Windows\\notepad.exe']
getSchemeAndHierarchy('file:///tmp/cache.db');      // ['file', '/tmp/cache.db']
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array{0: ?string, 1: string}

        
On this page

Search results