0

Anybody know if there is a way to merge to Paths easily?

/www/htdocs/v450687/server/jobs/bodymind/uploads
uploads/videoscontent/1/

to /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1/

/www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent
uploads/videoscontent/1/snips

to /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1/snips

/www/htdocs/v450687/server/jobs/bodymind/uploads/1/1
1/1/snips

to /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1/1/snips

dazzafact
  • 2,570
  • 3
  • 30
  • 49
  • 1
    Possible duplicate: [https://stackoverflow.com/questions/2945446/built-in-function-to-combine-overlapping-string-sequences-in-php](https://stackoverflow.com/questions/2945446/built-in-function-to-combine-overlapping-string-sequences-in-php) – RNH Feb 11 '20 at 10:13
  • Thanks! it seems to work perfectly! – dazzafact Feb 11 '20 at 10:18

3 Answers3

0

Solution: built in function to combine overlapping string sequences in php?

echo replaceOverlap("abxcdex", "xcdexfg"); //Result: abxcdexfg

function findOverlap($str1, $str2){
  $return = array();
  $sl1 = strlen($str1);
  $sl2 = strlen($str2);
  $max = $sl1>$sl2?$sl2:$sl1;
  $i=1;
  while($i<=$max){
    $s1 = substr($str1, -$i);
    $s2 = substr($str2, 0, $i);
    if($s1 == $s2){
      $return[] = $s1;
    }
    $i++;
  }
  if(!empty($return)){
    return $return;
  }
  return false;
}

function replaceOverlap($str1, $str2, $length = "long"){
  if($overlap = findOverlap($str1, $str2)){
    switch($length){
      case "short":
        $overlap = $overlap[0];
        break;
      case "long":
      default:
        $overlap = $overlap[count($overlap)-1];
        break;
    }     
    $str1 = substr($str1, 0, -strlen($overlap));
    $str2 = substr($str2, strlen($overlap));
    return $str1.$overlap.$str2;
  }
  return false;
}
dazzafact
  • 2,570
  • 3
  • 30
  • 49
0

As far as I know, there is no built-in method for this. Here is my approach:

  1. Explode the two paths into an array
  2. Merge them together
  3. Remove duplicates.

Here is an example:

$path_1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads';
$path_2 = 'uploads/videoscontent/1/';
echo implode('/', array_unique(array_merge(explode('/', $path_1), explode('/', $path_2)), SORT_REGULAR));

Output: /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1

Andrew
  • 827
  • 2
  • 6
  • 14
  • Works, but not with same names like: "/www/htdocs/v450687/server/jobs/bodymind/uploads/1/1" AND "1/1/snips" – dazzafact Feb 12 '20 at 11:18
0

You have to explode paths into arrays, merge them, and remove duplicates. You can do it in different ways, here are some examples:

$path1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads';
$path2 = 'uploads/videoscontent/1/';

print_r(pathToArray($path1, $path2));

function pathToArray($path1, $path2){
    foreach(explode('/', $path1) as $part){
        $output1[] = $part;
    }

    foreach(explode('/', $path2) as $part){
        $output2[] = $part;
    }

    $output = array_merge($output1, $output2);

    $output = array_unique($output);
    $output = implode("/",$output);
    return $output;
}

Or

$path1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads';
$path2 = 'uploads/videoscontent/1/';

echo implode('/', array_unique(array_merge(explode('/', $path_1), explode('/', $path_2)), SORT_REGULAR));

UPDATE: As I see you have updated your question, so I develop my answer. In this case to fix this, all you need to do is to use array_unique() for each array instead.

$path1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads/1/1';
$path2 = '1/1/snips';

print_r(pathToArray($path1, $path2));

function pathToArray($path1, $path2){
    foreach(explode('/', $path1) as $part){
        $output1[] = $part;
    }

    foreach(explode('/', $path2) as $part){
        $output2[] = $part;
    }

    $output1 = array_unique($output1);
    $output2 = array_unique($output2);
    $output = array_merge($output1, $output2);

    //$output = array_unique($output);
    $output = implode("/",$output);
    return $output;
}
Coder
  • 1
  • 1
  • Works, but not with same names like "/www/htdocs/v450687/server/jobs/bodymind/uploads/1/1" AND "1/1/snips" – dazzafact Feb 12 '20 at 11:16
  • you can use first function for your first 4 paths, while second function for others. You need to tell your code if you want to remove duplicates from whole merged array or from each array instead depends on your needs. You can use something like if else maybe! – Coder Feb 13 '20 at 09:42