Oihana PHP

extractCanonicalParts.php

Table of Contents

Functions

extractCanonicalParts()  : array<string|int, string>
Extracts the canonical path segments of « $pathWithoutRoot ».

Functions

extractCanonicalParts()

Extracts the canonical path segments of « $pathWithoutRoot ».

extractCanonicalParts(string $root, string $pathWithoutRoot) : array<string|int, string>

This method breaks down the input path into segments, and processes them to remove:

  • Redundant current directory references (.)
  • Properly resolvable parent references (..)

It does not access the filesystem and works purely on string logic.

Parameters
$root : string

The base root path. Used to determine whether .. segments can be collapsed. If empty, leading .. segments will be preserved.

$pathWithoutRoot : string

The input path (already stripped of the root part).

Tags
example
extractCanonicalParts('/var/www', 'project/../cache/./logs')
// Returns: ['cache', 'logs']

extractCanonicalParts('', '../../folder')
// Returns: ['..', '..', 'folder']
note

This function does not validate that the resulting path actually exists.

author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array<string|int, string>

An array of canonical path segments.


        
On this page

Search results