1

I want to move an old folder with all its contents to another new folder, however, I've to check that the new folder destination is not including the old folder destination otherwise I am trying to move the folder into itself which is impossible.

I want to check whether an old path already is a subset of a new path.

Something like:

check_function( "folder/test" , "folder/test/test2"); //true
check_function( "folder/test/" , "folder/test/test2"); //true

check_function( "folder/test" , "folder/test2"); //false
check_function( "folder/test" , "folder/test2/test3"); //false

How can I do this?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Power
  • 91
  • 8

2 Answers2

1
<?php

function sanitize(string $path): string
{
    return trim(trim($path), "/");
}

function check(string $path1, string $path2): bool
{
    $path1 = sanitize($path1);
    $path2 = sanitize($path2);

    $pathsElem1 = explode("/", $path1);
    $pathsElem2 = explode("/", $path2);

    foreach ($pathsElem1 as $i => $item) {
        if (!array_key_exists($i, $pathsElem2)) {
            return false;
        }

        if ($pathsElem2[$i] !== $item) {
            return false;
        }
    }

    return true;
}

$testCases = [
    check("folder/test", "folder/test/test2"),
    check("folder/test/", "folder/test/test2"),
    check("folder/test", "folder/test2"),
    check("folder/test", "folder/test2/test3"),
];

var_dump($testCases);

outputs:

array(4) {
  [0]=>
  bool(true)
  [1]=>
  bool(true)
  [2]=>
  bool(false)
  [3]=>
  bool(false)
}
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • You may have to safeguard non-existing index in the arrays. – k0pernikus Apr 30 '18 at 15:23
  • Nice answer, what do you mean by safeguard? – Power Apr 30 '18 at 15:27
  • 1
    @Power Try running with an empty path as second parameter, e.g. `check("foo/bar/baz", "")`, it will warn against `Undefined offset` because this implementation assumes `$path2` to be longer than `path1`, yet it should get you started. – k0pernikus Apr 30 '18 at 15:30
  • 1
    @Power I have updated my example to do an early return (no need to iterate the entire array if mismatch is already found) and added a safeguard against undefined index. – k0pernikus Apr 30 '18 at 15:47
  • Maybe instead for checking index with each index, just make sure that `if ( count($pathsElem1) > count($pathsElem2) ) { return false; }` out side the loop before it begins – Power Apr 30 '18 at 19:37
-1

Check if the strings have a / as the last character. If not, append it.

Then check if the second string starts with the first.

Here are some startsWith and endsWith functions to help:

startsWith() and endsWith() functions in PHP

geometrikal
  • 3,195
  • 2
  • 29
  • 40
  • Adding a slash seems a very good idea to unify the strings before comparison, but in some cases I will check files, is there an already programmed function to check if a path contains itself? – Power Apr 30 '18 at 15:16
  • I don't know about built in functions. Can you add an example of the file case to your question. – geometrikal Apr 30 '18 at 15:18