PHP Plagiarism Check
function plagiarismCheck($code1, $code2)
{
$c1 = implode(" ", $code1);
$c2 = implode(" ", $code2);
if ($c1 === $c2) {
return false;
}
$d1 = $d2 = $rCand = [];
preg_match_all('/[\w]+/', $c1, $d1);
preg_match_all('/[\w]+/', $c2, $d2);
foreach ($d1[0] as $k => $v) {
if ($v !== $d2[0][$k] && ! is_numeric($v)) {
$rCand[$v] = $d2[0][$k];
}
}
foreach ($rCand as $orig => $repl) {
$c1 = preg_replace("/(\W){$orig}(\W*)/", '$1PLACEHOLDER' . $orig . '$2', $c1);
$c1 = preg_replace("/(\W){$orig}/", '$1PLACEHOLDER' . $orig, $c1);
}
foreach ($rCand as $orig => $repl) {
$c1 = preg_replace("/(\W)PLACEHOLDER{$orig}(\W)/", '$1' . $repl . '$2', $c1);
$c1 = preg_replace("/(\W)PLACEHOLDER{$orig}/", '$1' . $repl, $c1);
}
return $c1 === $c2;
}
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.