Oihana PHP

canonicalizePath.php

Table of Contents

Functions

canonicalizePath()  : string
Converts any given path to a **canonical, absolute‑style** representation.

Functions

canonicalizePath()

Converts any given path to a **canonical, absolute‑style** representation.

canonicalizePath(string $path) : string

The algorithm:

  1. Early return / cache – looks up the path in a static LRU‑style buffer (see CanonicalizeBuffer).
  2. Home expansion – replaces a leading tilde ~ with the current user’s home directory (platform‑aware via getHomeDirectory()).
  3. Separator normalisation – back‑slashes ⇒ forward‑slashes via normalizePath().
  4. Root / remainder split – handled by splitPath().
  5. Dot & dot‑dot cleanup – collapses . and .. segments with extractCanonicalParts().
  6. Buffer write‑back – stores the result; periodically cleans the buffer after insertions.

No filesystem access is performed; non‑existent paths are allowed.

Parameters
$path : string

The path to canonicalise. May be relative or absolute, Windows or Unix, and may start with ~.

Tags
example
canonicalizePath('~/projects/../site//index.php');
// -> "/home/alice/site/index.php"   (Linux)

canonicalizePath('C:\\Temp\\..\\Logs\\.');
// -> "C:/Logs"
see
normalizePath()
see
splitPath()
see
extractCanonicalParts()
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

Canonicalised path with forward slashes and no redundant . / .. segments.


        
On this page

Search results