98

Is there a builtin function in PHP to intelligently join path strings? The function, given abc/de/ and /fg/x.php as arguments, should return abc/de/fg/x.php; the same result should be given using abc/de and fg/x.php as arguments for that function.

If not, is there an available class? It could also be valuable for splitting paths or removing parts of them. If you have written something, may you share your code here?

It is ok to always use /, I am coding for Linux only.

In Python there is os.path.join, which is great.

2
  • 6
    Note that your example is quite misleading since os.path.join('some/relative/path, '/an/absolute/path') will always return /an/absolute/path. So you're either looking for an os.path.join replacement (then fix your example) or something that is close to it, with the exception that absolute paths coming second (or nth) are treated as relative paths.
    – user948581
    Feb 24, 2014 at 10:59
  • 3
    @Tibo, and the upvoters of his comment: no, the example is not misleading at all, except if one ignores the gist of the question and insists on overemphasizing the the Python reference, which is a marginal remark for orientation. The question is perfectly clear about what it wants, which is not exactly os.path.join. Read: "The function, given abc/de/ and /fg/x.php as arguments, should return abc/de/fg/x.php".
    – Sz.
    Dec 22, 2017 at 23:17

26 Answers 26

153
function join_paths() {
    $paths = array();

    foreach (func_get_args() as $arg) {
        if ($arg !== '') { $paths[] = $arg; }
    }

    return preg_replace('#/+#','/',join('/', $paths));
}

My solution is simpler and more similar to the way Python os.path.join works

Consider these test cases

array               my version    @deceze      @david_miller    @mark

['','']             ''            ''           '/'              '/'
['','/']            '/'           ''           '/'              '/'
['/','a']           '/a'          'a'          '//a'            '/a'
['/','/a']          '/a'          'a'          '//a'            '//a'
['abc','def']       'abc/def'     'abc/def'    'abc/def'        'abc/def'
['abc','/def']      'abc/def'     'abc/def'    'abc/def'        'abc//def'
['/abc','def']      '/abc/def'    'abc/def'    '/abc/def'       '/abc/def'
['','foo.jpg']      'foo.jpg'     'foo.jpg'    '/foo.jpg'       '/foo.jpg'
['dir','0','a.jpg'] 'dir/0/a.jpg' 'dir/a.jpg'  'dir/0/a.jpg'    'dir/0/a.txt'
7
  • 5
    This is the best answer as it best matches the question - it is the closest to os.path.join and does intelligently join path strings. The answer could be improved by adding the "reference" implementation of os.path.join, and indicating the OP's specific that break the rule (test case ['abc','/def'] is wrong w.r.t os.path.join, but right as per the question).
    – user948581
    Feb 24, 2014 at 10:55
  • @qix why? Windows understands forward slashes just fine Dec 12, 2014 at 9:19
  • 4
    @qix look, I do understand your point, usually I'd agree with you, but realistically PHP isn't going to run on platforms that don't use slashes as path separator anytime soon, and to use the constant the preg_replace() would become a real mess (you'd need to escape the path separator in the regexp), so I choose this tradeoff. Dec 14, 2014 at 20:46
  • 4
    So you're cutting corners because you're lazy, even though string interpolation of constants in PHP is nearly free? Tsk. Dec 14, 2014 at 22:06
  • 3
    As a note, my team has just found a case where / separators don't work (an old PHP5.3.4 install on a Windows Server 2012 using the msys git shell). May 19, 2015 at 4:47
57

Since this seems to be a popular question and the comments are filling with "features suggestions" or "bug reports"... All this code snippet does is join two strings with a slash without duplicating slashes between them. That's all. No more, no less. It does not evaluate actual paths on the hard disk nor does it actually keep the beginning slash (add that back in if needed, at least you can be sure this code always returns a string without starting slash).

join('/', array(trim("abc/de/", '/'), trim("/fg/x.php", '/')));

The end result will always be a path with no slashes at the beginning or end and no double slashes within. Feel free to make a function out of that.

EDIT: Here's a nice flexible function wrapper for above snippet. You can pass as many path snippets as you want, either as array or separate arguments:

function joinPaths() {
    $args = func_get_args();
    $paths = array();
    foreach ($args as $arg) {
        $paths = array_merge($paths, (array)$arg);
    }

    $paths = array_map(create_function('$p', 'return trim($p, "/");'), $paths);
    $paths = array_filter($paths);
    return join('/', $paths);
}

echo joinPaths(array('my/path', 'is', '/an/array'));
//or
echo joinPaths('my/paths/', '/are/', 'a/r/g/u/m/e/n/t/s/');

:o)

13
  • 5
    function pj($a,$b) { return rtrim($a, '/') .'/'. ltrim($b, '/'); }
    – user89021
    Jul 7, 2009 at 9:52
  • 2
    This doesn't always work as described. joinPaths('', 'foo.jpg') becomes '/foo.jpg'. I noticed this after my php file manager started writing user-uplaoded files to the root of the filesystem! A corrected version should remove any paths that are empty strings.
    – EricP
    Jan 3, 2011 at 3:21
  • 37
    Should this be using DIRECTORY_SEPARATOR instead of '/'?
    – Dave
    Jan 13, 2012 at 20:10
  • 2
    @fe_ That's a completely different function from what the question was asking about.
    – deceze
    May 24, 2013 at 13:16
  • 4
    I do not agree as the person clearly notified that he used to results given by python's os.path.join which gives this result and which he finds great. So I do not believe that it is an other function. Just as join('/a/b','../c') should return /a/c without any external normalizing needed.
    – fe_lix_
    May 24, 2013 at 14:04
18

@deceze's function doesn't keep the leading / when trying to join a path that starts with a Unix absolute path, e.g. joinPaths('/var/www', '/vhosts/site');.

function unix_path() {
  $args = func_get_args();
  $paths = array();

  foreach($args as $arg) {
    $paths = array_merge($paths, (array)$arg);
  }

  foreach($paths as &$path) {
    $path = trim($path, '/');
  }

  if (substr($args[0], 0, 1) == '/') {
    $paths[0] = '/' . $paths[0];
  }

  return join('/', $paths);
}
0
17

My take:

function trimds($s) {
    return rtrim($s,DIRECTORY_SEPARATOR);
}

function joinpaths() {
    return implode(DIRECTORY_SEPARATOR, array_map('trimds', func_get_args()));
}

I'd have used an anonymous function for trimds, but older versions of PHP don't support it.

Example:

join_paths('a','\\b','/c','d/','/e/','f.jpg'); // a\b\c\d\e\f.jpg (on Windows)

Updated April 2013 March 2014 May 2018:

function join_paths(...$paths) {
    return preg_replace('~[/\\\\]+~', DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $paths));
}

This one will correct any slashes to match your OS, won't remove a leading slash, and clean up and multiple slashes in a row.

1
  • 13
    it always creates an absolute path, but at least somebody mentioned DIRECTORY_SEPARATOR... Nov 1, 2011 at 18:06
7

If you know the file/directory exists, you can add extra slashes (that may be unnecessary), then call realpath, i.e.

realpath(join('/', $parts));

This is of course not quite the same thing as the Python version, but in many cases may be good enough.

7

As a fun project, I created yet another solution. Should be universal for all operating systems.

For PHP 7.4+:

/**
 * Join string into a single URL string.
 *
 * @param string $parts,... The parts of the URL to join.
 * @return string The URL string.
 */
function join_paths(...$parts) {
    if (sizeof($parts) === 0) return '';
    $prefix = ($parts[0] === DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '';
    $processed = array_filter(
        array_map(fn ($part) => rtrim($part, DIRECTORY_SEPARATOR), $parts),
        fn ($part) => !empty($part)
    );
    return $prefix . implode(DIRECTORY_SEPARATOR, $processed);
}

For PHP 7.2+:

<?php

/**
 * Join string into a single URL string.
 *
 * @param string $parts,... The parts of the URL to join.
 * @return string The URL string.
 */
function join_paths(...$parts) {
    if (sizeof($parts) === 0) return '';
    $prefix = ($parts[0] === DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '';
    $processed = array_filter(array_map(function ($part) {
        return rtrim($part, DIRECTORY_SEPARATOR);
    }, $parts), function ($part) {
        return !empty($part);
    });
    return $prefix . implode(DIRECTORY_SEPARATOR, $processed);
}

For PHP version before 7.2:

/**
 * Join string into a single URL string.
 *
 * @param string $parts,... The parts of the URL to join.
 * @return string The URL string.
 */
function join_paths() {
    $parts = func_get_args();
    if (sizeof($parts) === 0) return '';
    $prefix = ($parts[0] === DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '';
    $processed = array_filter(array_map(function ($part) {
        return rtrim($part, DIRECTORY_SEPARATOR);
    }, $parts), function ($part) {
        return !empty($part);
    });
    return $prefix . implode(DIRECTORY_SEPARATOR, $processed);
}

Some test case for its behaviour.

// relative paths
var_dump(join_paths('hello/', 'world'));
var_dump(join_paths('hello', 'world'));
var_dump(join_paths('hello', '', 'world'));
var_dump(join_paths('', 'hello/world'));
echo "\n";

// absolute paths
var_dump(join_paths('/hello/', 'world'));
var_dump(join_paths('/hello', 'world'));
var_dump(join_paths('/hello/', '', 'world'));
var_dump(join_paths('/hello', '', 'world'));
var_dump(join_paths('', '/hello/world'));
var_dump(join_paths('/', 'hello/world'));

Results:

string(11) "hello/world"
string(11) "hello/world"
string(11) "hello/world"
string(11) "hello/world"

string(12) "/hello/world"
string(12) "/hello/world"
string(12) "/hello/world"
string(12) "/hello/world"
string(12) "/hello/world"
string(12) "/hello/world"

Update: Added a version that supports PHP before 7.2.

Update: Added a version for PHP 7.4+ with arrow function.

3
  • +1 Thanks alot @Koala Yeung. Your solution is compatible with PHP-versions above v7.2. The code from the accepted answer uses create_function() which is deprecated in versions above 7.2 - in 2013 we just had PHP v5.5 though. Jul 31, 2020 at 1:54
  • Thanks Koala Yeung! This improves nicely upon the quite useful solution by @Riccardo Galli, which was my previous go-to for the task. I can't find a way to break this one, and it's portable as long as your PHP is reasonably current. I'm happy with that.
    – knghtbrd
    Sep 25, 2021 at 13:34
  • 1
    @JackLeEmmerdeur: Added a version that works with older PHP. But the PHP 7.2+ is, IMO, cleaner to read. Sep 27, 2021 at 3:02
6

An alternative is using implode() and explode().

$a = '/a/bc/def/';
$b = '/q/rs/tuv/path.xml';

$path = implode('/',array_filter(explode('/', $a . $b)));

echo $path;  // -> a/bc/def/q/rs/tuv/path.xml
1
  • Try url /offset/0/limit/1.
    – Danon
    May 4, 2017 at 11:39
5

Elegant Python-inspired PHP one-liner way to join path.

This code doesn't use unnecessary array.

Multi-platform

function os_path_join(...$parts) {
  return preg_replace('#'.DIRECTORY_SEPARATOR.'+#', DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, array_filter($parts)));
}

Unix based systems

function os_path_join(...$parts) {
  return preg_replace('#/+#', '/', implode('/', array_filter($parts)));
}

Unix based system without REST parameters (don't respect explicit PEP8 philosophy) :

function os_path_join() {
  return preg_replace('#/+#', '/', implode('/', array_filter(func_get_args())));
}

Usage

$path = os_path_join("", "/", "mydir/", "/here/");

Bonus : if you want really follow Python os.path.join(). First argument is required :

function os_path_join($path=null, ...$paths) {
  if (!is_null($path)) {
    throw new Exception("TypeError: join() missing 1 required positional argument: 'path'", 1);
  }
  $path = rtrim($path, DIRECTORY_SEPARATOR);
  foreach ($paths as $key => $current_path) {
    $paths[$key] = $paths[$key] = trim($current_path, DIRECTORY_SEPARATOR);
  }
  return implode(DIRECTORY_SEPARATOR, array_merge([$path], array_filter($paths)));
}

Check os.path.join() source if you want : https://github.com/python/cpython/blob/master/Lib/ntpath.py

Warning : This solution is not suitable for urls.

3

The solution below uses the logic proposed by @RiccardoGalli, but is improved to avail itself of the DIRECTORY_SEPARATOR constant, as @Qix and @FélixSaparelli suggested, and, more important, to trim each given element to avoid space-only folder names appearing in the final path (it was a requirement in my case).

Regarding the escape of directory separator inside the preg_replace() pattern, as you can see I used the preg_quote() function which does the job fine.
Furthermore, I would replace mutiple separators only (RegExp quantifier {2,}).

// PHP 7.+
function paths_join(string ...$parts): string {
    $parts = array_map('trim', $parts);
    $path = [];

    foreach ($parts as $part) {
        if ($part !== '') {
            $path[] = $part;
        }
    }

    $path = implode(DIRECTORY_SEPARATOR, $path);

    return preg_replace(
        '#' . preg_quote(DIRECTORY_SEPARATOR) . '{2,}#',
        DIRECTORY_SEPARATOR,
        $path
    );
}
2

for getting parts of paths you can use pathinfo https://www.php.net/manual/en/function.pathinfo.php

for joining the response from @deceze looks fine

2

A different way of attacking this one:

function joinPaths() {
  $paths = array_filter(func_get_args());
  return preg_replace('#/{2,}#', '/', implode('/', $paths));
}
1

This is a corrected version of the function posted by deceze. Without this change, joinPaths('', 'foo.jpg') becomes '/foo.jpg'

function joinPaths() {
    $args = func_get_args();
    $paths = array();
    foreach ($args as $arg)
        $paths = array_merge($paths, (array)$arg);

    $paths2 = array();
    foreach ($paths as $i=>$path)
    {   $path = trim($path, '/');
        if (strlen($path))
            $paths2[]= $path;
    }
    $result = join('/', $paths2); // If first element of old path was absolute, make this one absolute also
    if (strlen($paths[0]) && substr($paths[0], 0, 1) == '/')
        return '/'.$result;
    return $result;
}
1

This seems to be work quite well, and looks reasonably neat to me.

private function JoinPaths() {
  $slash = DIRECTORY_SEPARATOR;
  $sections = preg_split(
          "@[/\\\\]@",
          implode('/', func_get_args()),
          null,
          PREG_SPLIT_NO_EMPTY);
  return implode($slash, $sections);
}
1

Best solution found:

function joinPaths($leftHandSide, $rightHandSide) { 
    return rtrim($leftHandSide, '/') .'/'. ltrim($rightHandSide, '/'); 
}

NOTE: Copied from the comment by user89021

1

OS-independent version based on the answer by mpen but encapsulated into a single function and with the option to add a trailing path separator.

function joinPathParts($parts, $trailingSeparator = false){
    return implode(
        DIRECTORY_SEPARATOR, 
        array_map(
            function($s){
                return rtrim($s,DIRECTORY_SEPARATOR);
            }, 
            $parts)
        )
        .($trailingSeparator ? DIRECTORY_SEPARATOR : '');
}

Or for you one-liner lovers:

function joinPathParts($parts, $trailingSeparator = false){
    return implode(DIRECTORY_SEPARATOR, array_map(function($s){return rtrim($s,DIRECTORY_SEPARATOR);}, $parts)).($trailingSeparator ? DIRECTORY_SEPARATOR : '');
}

Simply call it with an array of path parts:

// No trailing separator - ex. C:\www\logs\myscript.txt
$logFile = joinPathParts([getcwd(), 'logs', 'myscript.txt']);

// Trailing separator - ex. C:\www\download\images\user1234\
$dir = joinPathParts([getcwd(), 'download', 'images', 'user1234'], true);
1

Note OP is asking for something slightly different from https://docs.python.org/3/library/os.path.html#os.path.join which does more than just join paths with the right number of separators.

While what they have asked for has been answered, for anyone skim reading the Q&A, there will be the following differences and ambiguous cases between what was asked for and os.path.join():

Many of the above solutions don't work for the root only case ['/'] => '/'

os.path.join drop all args to the left of the rightmost absolute path e.g. ['a', 'b', '/c'] => '/c' which to be fair is probably not the behaviour you want if you are refactoring existing php which has a lot of path segments appear like they are absolute paths.

Another difference with os.path.join is it won't drop additional separators within a single string ['a///', 'b', 'c'] => 'a///b/c'

Another special case is one or more empty strings resulting in a trailing slash for os.path.join: ['a', ''] or ['a', '', ''] => 'a/'

0

Here's a function that behaves like Node's path.resolve:

function resolve_path() {
    $working_dir = getcwd();
    foreach(func_get_args() as $p) {
        if($p === null || $p === '') continue;
        elseif($p[0] === '/') $working_dir = $p;
        else $working_dir .= "/$p";
    }
    $working_dir = preg_replace('~/{2,}~','/', $working_dir);
    if($working_dir === '/') return '/';
    $out = [];
    foreach(explode('/',rtrim($working_dir,'/')) as $p) {
        if($p === '.') continue;
        if($p === '..') array_pop($out);
        else $out[] = $p;
    }
    return implode('/',$out);
}

Test cases:

resolve_path('/foo/bar','./baz')         # /foo/bar/baz
resolve_path('/foo/bar','/tmp/file/')    # /tmp/file
resolve_path('/foo/bar','/tmp','file')   # /tmp/file
resolve_path('/foo//bar/../baz')         # /foo/baz
resolve_path('/','foo')                  # /foo
resolve_path('/','foo','/')              # /
resolve_path('wwwroot', 'static_files/png/', '../gif/image.gif') 
                                  # __DIR__.'/wwwroot/static_files/gif/image.gif'
0

From the great answer of Ricardo Galli, a bit of improvement to avoid killing the protocol prefix.

The idea is to test for the presence of a protocol in one argument, and maintain it into the result. WARNING: this is a naive implementation!

For example:

array("http://domain.de","/a","/b/")

results to (keeping protocol)

"http://domain.de/a/b/"

instead of (killing protocol)

"http:/domain.de/a/b/"

But http://codepad.org/hzpWmpzk needs a better code writing skill.

0

I love Riccardo's answer and I think it is the best answer.

I am using it to join paths in url building, but with one small change to handle protocols' double slash:

function joinPath () {
    $paths = array();

    foreach (func_get_args() as $arg) {
        if ($arg !== '') { $paths[] = $arg; }
    }

    // Replace the slash with DIRECTORY_SEPARATOR
    $paths = preg_replace('#/+#', '/', join('/', $paths));
    return preg_replace('#:/#', '://', $paths);
}
0
function path_combine($paths) {
  for ($i = 0; $i < count($paths); ++$i) {
    $paths[$i] = trim($paths[$i]);
  }

  $dirty_paths = explode(DIRECTORY_SEPARATOR, join(DIRECTORY_SEPARATOR, $paths));
  for ($i = 0; $i < count($dirty_paths); ++$i) {
    $dirty_paths[$i] = trim($dirty_paths[$i]);
  }

  $unslashed_paths = array();

  for ($i = 0; $i < count($dirty_paths); ++$i) {
    $path = $dirty_paths[$i];
    if (strlen($path) == 0) continue;
    array_push($unslashed_paths, $path);
  }

  $first_not_empty_index = 0;
  while(strlen($paths[$first_not_empty_index]) == 0) {
    ++$first_not_empty_index;
  }
  $starts_with_slash = $paths[$first_not_empty_index][0] == DIRECTORY_SEPARATOR;

  return $starts_with_slash
    ? DIRECTORY_SEPARATOR . join(DIRECTORY_SEPARATOR, $unslashed_paths)
    : join(DIRECTORY_SEPARATOR, $unslashed_paths);
}

Example usage:

$test = path_combine([' ', '/cosecheamo', 'pizze', '///// 4formaggi', 'GORGONZOLA']);
echo $test;

Will output:

/cosecheamo/pizze/4formaggi/GORGONZOLA
0

Here is my solution:

function joinPath(): string {

        $path = '';
        foreach (func_get_args() as $numArg => $arg) {

            $arg = trim($arg);

            $firstChar = substr($arg, 0, 1);
            $lastChar = substr($arg, -1);

            if ($numArg != 0 && $firstChar != '/') {
                $arg = '/'.$arg;
                }

            # Eliminamos el slash del final
            if ($lastChar == '/') {
                $arg = rtrim($arg, '/');
                }

            $path .= $arg;
            }

        return $path;
        }
0

Hmmm most seem a bit over complicated. Dunno, this is my take on it:

// Takes any amount of arguments, joins them, then replaces double slashes
function join_urls() {
   $parts = func_get_args();
   $url_part = implode("/", $parts);
   return preg_replace('/\/{1,}/', '/', $url_part);
}
0

For people who want a join function that does the Windows backslash and the Linux forward slash.

Usage:

<?php
use App\Util\Paths
echo Paths::join('a','b'); //Prints 'a/b' on *nix, or 'a\\b' on Windows

Class file:

<?php
namespace App\Util;

class Paths
{
  public static function join_with_separator($separator, $paths) {
    $slash_delimited_path = preg_replace('#\\\\#','/', join('/', $paths));
    $duplicates_cleaned_path = preg_replace('#/+#', $separator, $slash_delimited_path);
    return $duplicates_cleaned_path;
  }

  public static function join() {
    $paths = array();

    foreach (func_get_args() as $arg) {
      if ($arg !== '') { $paths[] = $arg; }
    }
    return Paths::join_with_separator(DIRECTORY_SEPARATOR, $paths);
  }
}

Here's the test function:

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use App\Util\Paths;

class PathsTest extends TestCase
{
  public function testWindowsPaths()
  {
    $TEST_INPUTS = [
      [],
      ['a'],
      ['a','b'],
      ['C:\\','blah.txt'],
      ['C:\\subdir','blah.txt'],
      ['C:\\subdir\\','blah.txt'],
      ['C:\\subdir','nested','1/2','blah.txt'],
    ];
    $EXPECTED_OUTPUTS = [
      '',
      'a',
      'a\\b',
      'C:\\blah.txt',
      'C:\\subdir\\blah.txt',
      'C:\\subdir\\blah.txt',
      'C:\\subdir\\nested\\1\\2\\blah.txt',
    ];
    for ($i = 0; $i < count($TEST_INPUTS); $i++) {
      $actualPath = Paths::join_with_separator('\\', $TEST_INPUTS[$i]);
      $expectedPath = $EXPECTED_OUTPUTS[$i];
      $this->assertEquals($expectedPath, $actualPath);
    }
  }
  public function testNixPaths()
  {
    $TEST_INPUTS = [
      [],
      ['a'],
      ['a','b'],
      ['/home','blah.txt'],
      ['/home/username','blah.txt'],
      ['/home/username/','blah.txt'],
      ['/home/subdir','nested','1\\2','blah.txt'],
    ];
    $EXPECTED_OUTPUTS = [
      '',
      'a',
      'a/b',
      '/home/blah.txt',
      '/home/username/blah.txt',
      '/home/username/blah.txt',
      '/home/subdir/nested/1/2/blah.txt',
    ];
    for ($i = 0; $i < count($TEST_INPUTS); $i++) {
      $actualPath = Paths::join_with_separator('/', $TEST_INPUTS[$i]);
      $expectedPath = $EXPECTED_OUTPUTS[$i];
      $this->assertEquals($expectedPath, $actualPath);
    }
  }
}
0
$args = [sys_get_temp_dir(), "path1","path2", "filename.pdf"];
$filename = implode( DIRECTORY_SEPARATOR, $args);
// output "C:\Users\User\AppData\Local\Temp\path1\path2\filename.pdf"
0

This one gives the same output as Riccardo Galli's answer, with the following improvements:

  • Accept nested arrays of subpaths, recursively;
  • Accept null values correctly.
function joinPaths(string|array|null ...$paths): string
{
    $flatPaths = [];
    array_walk_recursive($paths, function ($p) use (&$flatPaths) { $flatPaths[] = $p; });
    $paths = array_filter($flatPaths, fn ($p) => '' !== $p && null !== $p);

    return preg_replace('+/{2,}+', '/', implode('/', $paths));
}
----------------- Similar behaviours ----------------

Array                  My version     @RiccardoGalli

['','']                ''             ''
['','/']               '/'            '/'
['/','a']              '/a'           '/a'
['/','/a']             '/a'           '/a'
['abc','def']          'abc/def'      'abc/def'
['abc','/def']         'abc/def'      'abc/def'
['/abc','def']         '/abc/def'     '/abc/def'
['','foo.jpg']         'foo.jpg'      'foo.jpg'
['dir','0','a.jpg']    'dir/0/a.jpg'  'dir/0/a.jpg'

----------------- Improvements ----------------------

Array                  My version     @RiccardoGalli

[null,'foo.jpg']       'foo.jpg'      '/foo.jpg'
['dir',['0','a.jpg']]  'dir/0/a.jpg'  ERROR
[[], []]               ''             ERROR
[[['a']],[],null,'b']  'a/b'          ERROR
-9

I liked several solutions presented. But those who does replacing all '/+' into '/' (regular expressions) are forgetting that os.path.join() from python can handle this kind of join:

os.path.join('http://example.com/parent/path', 'subdir/file.html')

Result: 'http://example.com/parent/path/subdir/file.html'

1
  • 2
    Question asked for PHP, answer is for Python
    – Toilal
    Sep 27, 2017 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.