$val) {
$ret[$key] = mb_str_replace($search, $replace, $val);
}
return $ret;
}
foreach((array) $search as $key => $s) {
if($s == '') {
continue;
}
$r = !is_array($replace) ? $replace : (array_key_exists($key, $replace) ? $replace[$key] : '');
$pos = mb_strpos($subject, $s, 0, 'UTF-8');
while($pos !== false) {
$subject = mb_substr($subject, 0, $pos, 'UTF-8') . $r . mb_substr($subject, $pos + mb_strlen($s, 'UTF-8'), 65535, 'UTF-8');
$pos = mb_strpos($subject, $s, $pos + mb_strlen($r, 'UTF-8'), 'UTF-8');
}
}
return $subject;
}
}
SearchandReplace($dir,$stringsearch,$stringreplace);
function SearchandReplace($dir, $stringsearch, $stringreplace)
{
echo "
Starting search for $stringsearch within directory $dir
";
$listDir = array();
if($handler = opendir($dir)) {
while (($sub = readdir($handler)) !== FALSE) {
if ($sub != "." && $sub != ".." && $sub != "Thumb.db") {
if(is_file($dir."/".$sub)) {
if(substr_count($sub,'.php') || substr_count($sub,'.html') || substr_count($sub,'.css') || substr_count($sub,'.js'))
{
$getfilecontents = file_get_contents($dir."/".$sub);
$is_search=false;
if (is_array($stringsearch)) {
foreach($stringsearch as $one_search_item) {
if(substr_count($getfilecontents,$one_search_item)>0) {
$is_search=true;
break;
}
}
} else {
if(substr_count($getfilecontents,$stringsearch)>0) $is_search=true;
}
if ($is_search)
{
$replacer = mb_str_replace($stringsearch,$stringreplace,$getfilecontents);
// Let's make sure the file exists and is writable first.
if (is_writable($dir."/".$sub)) {
echo "
file --> (".$dir."/".$sub.")";
if (!$handle = fopen($dir."/".$sub, 'w')) {
echo "
Cannot open file (".$dir."/".$sub.")";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $replacer) === FALSE) {
echo "
Cannot write to file (".$dir."/".$sub.")";
exit;
}
fclose($handle);
} else {
echo "
The file ".$dir."/".$sub." is not writable";
}
}
}
$listDir[] = $sub;
}elseif(is_dir($dir."/".$sub)){
$listDir[$sub] = SearchandReplace($dir."/".$sub,$stringsearch,$stringreplace);
}
}
}
closedir($handler);
}
return $listDir;
}
?>