Skip to content

Instantly share code, notes, and snippets.

@alecgorge
Created September 4, 2010 04:38
Show Gist options
  • Save alecgorge/564904 to your computer and use it in GitHub Desktop.
Save alecgorge/564904 to your computer and use it in GitHub Desktop.
function calcMostCommonDiff($orig, $files) {
$mismatches = array();
$inRow = 0;
$firstLength = strlen(reset($orig));
$firstFile = reset($orig);
foreach($files as $file) {
$inRow = 0;
for($i = 0; $i < $firstLength; $i++) {
$thisChar = $firstFile{$i};
if($i >= strlen($file)) continue;
$otherChar = $file{$i};
if($thisChar != $otherChar && is_numeric($thisChar) && is_numeric($otherChar)) {
$inRow++;
$mismatches[] = $i;
if($inRow === 3) {
$removeStart = count($mismatches);
unset($mismatches[$removeStart - 1]);
unset($mismatches[$removeStart - 2]);
unset($mismatches[$removeStart - 3]);
break;
}
}
else {
$inRow = 0;
}
}
}
$count = array();
$len = count($mismatches);
foreach($mismatches as $match) {
$count[$match]++;
}
arsort($count);
$count = array_slice(array_keys($count), 0, 2);
list($pos1, $pos2) = $count;
$ePos = ($pos1 > $pos2 ? $pos1 : $pos2);
foreach(array_insert($files, reset(array_keys($orig)), reset($orig)) as $file) {
$parts = explode('.', $file);
$extension = array_pop($parts);
$reconstructed = implode('.', $parts);
$frontRemoved = ltrim(substr($reconstructed, $ePos+1), ' .,-[]_');
$backRemoved = preg_replace("/(\[[a-z0-9]+\])/", "", $frontRemoved);
$names[] = trim($backRemoved);
}
return array($count, $mismatches, $names);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment