<?php

// Function to scan directory and its subdirectories
function scanDirectory($dir) {
    $files = [];
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));

    foreach ($iterator as $fileinfo) {
        if ($fileinfo->isFile()) {
            $extension = pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION);
            if (in_array($extension, ['html', 'php', 'js','css'])) {       
                $files[] = $fileinfo->getPathname();
            }
        }
    }

    return $files;
}


// Function to remove specific text pattern from a JavaScript file
function removeTextFromFile($file, $idx1) {
    $content = file_get_contents($file);

    if (strpos($content, $idx1) !== false) {
        $cleanContent = str_replace($idx1, '', $content);
        file_put_contents($file, $cleanContent);
        return true; // Text successfully removed
    } else {
        return false; // Text not found
    }
}

// Pattern and indices to match

$idx1 = '';  // Example start index

// Directory to scan
$directoryToScan = './';

// Get all relevant files in the directory and subdirectories
$files = scanDirectory($directoryToScan);

// Clean each file
foreach ($files as $file) {
    if (removeTextFromFile($file, $idx1)) {
        echo "Removed text from: $file\n";        
    }
}
echo "Cleaning completed.\n";