Tetris-ing an array

后端 未结 16 1416
时光取名叫无心
时光取名叫无心 2021-01-30 15:38

Consider the following array:

/www/htdocs/1/sites/lib/abcdedd
/www/htdocs/1/sites/conf/xyz
/www/htdocs/1/sites/conf/abc/         


        
16条回答
  •  暖寄归人
    2021-01-30 16:02

    $common = PHP_INT_MAX;
    foreach ($a as $item) {
            $common = min($common, str_common($a[0], $item, $common));
    }
    
    $result = array();
    foreach ($a as $item) {
            $result[] = substr($item, $common);
    }
    print_r($result);
    
    function str_common($a, $b, $max)
    {
            $pos = 0;
            $last_slash = 0;
            $len = min(strlen($a), strlen($b), $max + 1);
            while ($pos < $len) {
                    if ($a{$pos} != $b{$pos}) return $last_slash;
                    if ($a{$pos} == '/') $last_slash = $pos;
                    $pos++;
            }
            return $last_slash;
    }
    

提交回复
热议问题