Merge the line with the common middle part

I have two lines:

$a = '/srv/http/projects/name';
$b = '/projects/name/some/dir';

And I would like to get a concatenated string with a non-repeating common part:

$c = '/srv/http/projects/name/some/dir';

Is there an effective way to get it?

+3
source share
5 answers

Nothing I know because of the box. but this should do it:

function merge_overlap($left, $right) {
  // continue checking larger portions of $right
  for($l = 1; $l < strlen($right); $l++) {
    // if we no longer have a matching subsection return what left appended
    if(strpos($left, substr($right, 0, $l)) === false) {
      return $left . substr($right, $l - 1);
    }
  }

  // no overlap, return all
  return $left . $right;
}

EDIT: OBO has been updated.

UPDATE: this is not a solution, strpos () matches part of the text anywhere in the left path, should be compared with the tail.

Here is the correct implementation for my approach:

function merge_overlap($left, $right) {
  $l = strlen($right);
  // keep checking smaller portions of right
  while($l > 0 && substr($left, $l * -1) != substr($right, 0, $l))
    $l--;

  return $left . substr($right, $l);
}
+1
source

This is ugly and assumes your lines always begin with '/' ... but:

$a = '/srv/http/projects/name';
$b = '/projects/name/some/dir';

$merged = array_merge(explode('/', $a), explode('/', $b) );
$unique = array_unique($merged);

$c = implode('/', $unique);

print $c; // prints "/srv/http/projects/name/some/dir"
+1
source

function f($a, $b)
{
  for($i=0; count($a) > $i ; $i++)
  {
    if(strpos($b, substr($a, $i)) !== FALSE)
      return substr($a, 0, $i-1).$b;
  } 
  return $a.$b;
}
+1

:

function merge($a, $b) {
    // divide into path parts to compare the parts
    $start = preg_split('%/%', $a, -1, PREG_SPLIT_NO_EMPTY);
    $end = preg_split('%/%', $b, -1, PREG_SPLIT_NO_EMPTY);

    // if the first part of the first path is in the second path than switch
    if(in_array($start[0], $end)) {
       $temp = $start;
       $start = $end;
       $end = $temp;
    }
    $parts = array();
    // get the index of the last part of the first path in the second path
    $index = array_search($start[count($start)-1], $end);

    // if the part exists, remove the first parts of the second path
    if($index !== false) {
        $parts = array_merge($start, array_slice($end, $index+1));
    }
    return '/' . join('/', $parts);
}


$a = '/srv/http/projects/name';
$b = '/projects/name/some/dir';

print merge($a, $b);

:

/srv/http/projects/name/some/dir

, - , .

0
source

I don’t think there is a “smart” way to do this. Just iterate over $auntil all the leftmost tokens match the number of tokens at the beginning $b.

Here I have explode()both lines, so that I can easily fix the correct number of tokens through array_slice().

$a = '/srv/http/projects/name/http';
$b = '/projects/name/http/some/dir';

var_dump(merge_path($a, $b));

function merge_path($path1, $path2)
{
    $p1 = explode('/', trim($path1,' /'));
    $p2 = explode('/', trim($path2,' /'));

    $len = count($p1);

    do
    {
        if (array_slice($p1, -$len) === array_slice($p2, 0, $len))
        {
            return '/'
                 . implode('/', array_slice($p1, 0, -$len))
                 . '/'
                 . implode('/', $p2);
        }
    }
    while (--$len);

    return false;
}
0
source

Source: https://habr.com/ru/post/1732875/


All Articles