Skip to content

Core\Path::normalize

Core\Path::normalize(string $path): stringCore\Path::normalize(
    string $path,
): string

Removes the . and .. parts from a path, so the same place is always written the same way. It reads both / and \ as separators, and it never looks at the disk.

A . part means "this folder", so it is removed. A .. part means "the parent folder", so it removes the folder before it. /var/www/../log/./app.log gives /var/log/app.log. Repeated separators are removed too.

A .. at the start of a relative path stays, so ../shared stays ../shared. A .. above a root is removed, because a root has no parent. The empty path gives .. The result is written with Core\Path::SEPARATOR.

The result can still start with .., so normalize does not make a path safe. Core\IO::within checks that.

The examples below clean up a path, show the .. parts that stay, and find the paths in a list that name the same folder.

Parameters

  • $pathstringThe path. / and \ are both separators on every platform.

Return value

Returns string. The same path without . parts, and with each .. removing the folder before it. A .. at the start of a relative path stays. A .. that would go above a root is removed, because a root has no parent. A repeated separator or one at the end is removed. The empty path gives ..

Examples

Cleaning up a path

<?nvs
// Shows how `Core\Path::normalize` removes the `.` and `..` parts from a path.
// On Windows, the result is written with `\`. This example replaces it with `/`,
// so the output is the same on every platform.
class Show
{
public static function normal(string $path): void
{
echo Core\Str::replace(Core\Path::normalize($path), Core\Path::SEPARATOR, '/'), "\n";
}
}
// `..` removes `www`, and `.` is removed. This prints `/var/log/app.log`.
Show::normal('/var/www/../log/./app.log');
// Repeated separators and the one at the end are removed. This prints `shop/images/logo.png`.
Show::normal('shop//images/./logo.png/');
// A `\` is a separator too. This prints `C:/Users/report.txt`.
Show::normal("C:\\Users\\Public\\..\\report.txt");
Output
/var/log/app.log
shop/images/logo.png
C:/Users/report.txt

The parts that stay

<?nvs
// Shows what `Core\Path::normalize` does with a `..` that has no folder before it.
class Show
{
public static function normal(string $path): void
{
echo Core\Str::replace(Core\Path::normalize($path), Core\Path::SEPARATOR, '/'), "\n";
}
}
// A relative path keeps a `..` at its start. This prints `../../shared/logo.png`.
Show::normal('../../shared/logo.png');
// Here `..` removes `a`, and the second `..` stays. This prints `..`.
Show::normal('a/../..');
// A root has no parent, so `..` above it is removed. This prints `/`.
Show::normal('/srv/../..');
// The empty path means "this folder". This prints `.`.
Show::normal('');
Output
../../shared/logo.png
..
/
.

Folders listed twice

<?nvs
// Shows how to find the folders in a list that are written differently but are the same folder.
// In a real program, this list comes from a configuration file.
array<string> $folders = ['templates', './templates/', 'shop/../templates', 'shop/views', 'shop//views'];
// The normal form is the key, so two ways of writing one folder give one key.
array<string> $first = [];
foreach ($folders as string $folder) {
string $key = Core\Str::replace(Core\Path::normalize($folder), Core\Path::SEPARATOR, '/');
if (Core\Arr::hasKey($first, $key)) {
echo "\"", $folder, "\" is the same folder as \"", $first[$key], "\"\n";
} else {
$first[$key] = $folder;
}
}
// Five entries name two folders. This prints `2`.
echo Core\Arr::count($first), "\n";
Output
"./templates/" is the same folder as "templates"
"shop/../templates" is the same folder as "templates"
"shop//views" is the same folder as "shop/views"
2