All pastes #911736 Raw Edit

buzibob

public text v1 · immutable
·published 2008-02-20 22:07 UTC
rendered paste body
/**
url1 = http://google.com/search?q=apples
url2 = http://google.com/search?button=Search&q=bananas
url3 = http://google.com/search?q=oranges


from the upper inputs, I want to get the following output:
http://google.com/search?

question from Jaymon...
what happens if the third url is a little different?
url1 = http://google.com/search?q=apples
url2 = http://google.com/search?button=Search&q=bananas
url3 = http://goxgle.com/search?q=oranges

would you want the function to just return http://go ?
**/

// this should work...
$arr = array();
$arr[] = 'http://google.com/search?q=apples';
$arr[] = 'http://google.com/search?button=Search&q=bananas';
$arr[] = 'http://goxgle.com/search?q=oranges';

$common = getSame($arr);
echo $common,'<br />';

function getSame($list){

  // sanity...
  if(empty($list)){ return ''; }//if
  
  $common = $list[0];
  $total = count($list);
  $current = 1;
  
  while($current < $total){
  
    $first = $common;
    $second = $list[$current];
    $index = 0;
    $common = '';
    
    while(!empty($first[$index]) && !empty($second[$index])){
      if($first[$index] == $second[$index]){
        $common .= $first[$index];
      }else{
        // sicne we found something that didn't match, end looking...
        break;
      }//if/else
      $index++;
    }//while
  
    $current++;
  
  }//while

  return $common;

}//method