Skip to content

Core\Path::split

Splits a path into its parts: the folders and the file name, in order. It reads both / and \ as separators.

The method works on the text of the path and never looks at the disk. srv/shop/logo.png gives three parts: srv, shop and logo.png. The result never has an empty part. A separator that is repeated, or one at the end, adds nothing. The empty path gives an empty array.

When the path starts at a root, the root is the first part. The root is /, a drive such as C:\, or a network share such as \\server\share\. It is written with Core\Path::SEPARATOR, which is \ on Windows and / everywhere else. So /srv/shop gives /, srv and shop.

Because the root is kept, Core\Path::join can put the parts back together into the same path.

The examples below split a path, show the root and the empty path, and build the navigation links for a page from its path.

Parameters

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

Return value

Returns array<string>. The parts in order. When the path starts at a root, the root is the first part: /, a drive such as C:\, or a share such as \\server\share\. The root is written with Core\Path::SEPARATOR. No part is empty, because a repeated separator or one at the end adds nothing. The empty path gives an empty array.

Examples

The parts of a path

<?nvs
// Shows how to split a path into its folders and its file name with `Core\Path::split`.
// This prints `srv`, `shop`, `invoices` and `invoice-1042.pdf`, one per line.
foreach (Core\Path::split('srv/shop/invoices/invoice-1042.pdf') as string $part) {
echo $part, "\n";
}
// `/` and `\` are both separators, and a repeated one adds nothing.
// This prints `3`.
echo Core\Arr::count(Core\Path::split("logs\\2026//app.log")), "\n";
Output
srv
shop
invoices
invoice-1042.pdf
3

The root comes first

<?nvs
// Shows that the root of a path is the first part that `Core\Path::split` returns.
// On Windows, the root is written with `\`. This example replaces it with `/`,
// so the output is the same on every platform.
class Show
{
public static function parts(string $path): void
{
foreach (Core\Path::split($path) as string $part) {
echo '[', Core\Str::replace($part, Core\Path::SEPARATOR, '/'), '] ';
}
echo "\n";
}
}
// This prints `[/] [srv] [shop]`.
Show::parts('/srv/shop/');
// A drive is a root too. This prints `[C:/] [Users] [report.txt]`.
Show::parts("C:\\Users\\report.txt");
// The empty path has no parts, so this prints an empty line.
Show::parts('');
Output
[/] [srv] [shop]
[C:/] [Users] [report.txt]

Links to the parent pages

<?nvs
// Shows how to build the links to the parent pages of a page, from the path of its file.
class Breadcrumbs
{
public static function render(string $page): string
{
string $links = '';
string $url = '';
// Each folder gets a link to its own address, and the page itself gets none.
array<string> $parts = Core\Path::split($page);
uint $left = Core\Arr::count($parts);
foreach ($parts as string $part) {
$url = $url . '/' . $part;
$left = $left - 1;
if ($left > 0) {
$links = $links . "<a href=\"{$url}\">{$part}</a> > ";
} else {
$links = $links . $part;
}
}
return $links;
}
}
// This prints `<a href="/guide">guide</a> > <a href="/guide/install">install</a> > linux.md`.
echo Breadcrumbs::render('guide/install/linux.md'), "\n";
Output
<a href="/guide">guide</a> > <a href="/guide/install">install</a> > linux.md