This code was finally used before some manual tweaking:
function main() {
$files = array();
exec('grep -isr "\$Id" * | cut -d":" -f1', $files);
foreach ($files as $file) {
$ext = substr($file, strrpos($file, '.') + 1);
if (!in_array($ext, array('txt', 'php', 'sh', 'js', 'sql', 'inc', 'phpsh', 'html', 'htm', 'css', 'xml'))) {
continue;
}
if ($file === basename(FILE)) {
continue;
}
remove_id($file);
}
}
function remove_id($file) {
$isPHP = substr($file, -4) === '.php';
$content = file_get_contents($file);
$lines = explode("\n", $content);
$newLines = array();
$previousLine = '';
foreach ($lines as $line) {
if (preg_match('/\$Id/', $line)) {
if (trim($previousLine) === '*' || trim($previousLine) === '#') {
array_pop($newLines);
}
continue;
}
$newLines[] = $line;
$previousLine = $line;
}
$content = implode("\n", $newLines);
$fd = fopen($file, 'wb');
$res = fwrite($fd, $content);
fclose($fd);
}
main();
?>