Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Path.php

function Path::join

Joins two or more path strings into a canonical path.

File

vendor/symfony/filesystem/Path.php, line 653

Class

Path
Contains utility methods for handling path strings.

Namespace

Symfony\Component\Filesystem

Code

public static function join(string ...$paths) : string {
    $finalPath = null;
    $wasScheme = false;
    foreach ($paths as $path) {
        if ('' === $path) {
            continue;
        }
        if (null === $finalPath) {
            // For first part we keep slashes, like '/top', 'C:\' or 'phar://'
            $finalPath = $path;
            $wasScheme = str_contains($path, '://');
            continue;
        }
        // Only add slash if previous part didn't end with '/' or '\'
        if (!\in_array(substr($finalPath, -1), [
            '/',
            '\\',
        ], true)) {
            $finalPath .= '/';
        }
        // If first part included a scheme like 'phar://' we allow \current part to start with '/', otherwise trim
        $finalPath .= $wasScheme ? $path : ltrim($path, '/');
        $wasScheme = false;
    }
    if (null === $finalPath) {
        return '';
    }
    return self::canonicalize($finalPath);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal