A script that performs a search and replace function. Written because I didn't have one at the time. Can perform over multiple files and/or directories. Can be configured to ignore certain lines.
Download
View Source (print view)
Rating : 4.7
Views : 431
<?php
error_reporting(63);
/***************************************
** Title........: Search and replace utility
** Filename.....: search.replace.php
** Author.......: Richard Heyes
** Version......: 0.9
** Notes........:
** Last changed.: 23/04/2000
** Last change..:
***************************************/
$find = ''; // Find this.
$replace = ''; // And replace with this.
$ignore_lines = ''; // Can't use commas in this.
$directories = ''; // Comma seperated if multiple
$files = ''; // Search and replace in these files.
$include_subdir = 0; // Include subdirectories or not. 1/0
/***************************************
** The main search and replace routine.
***************************************/
function search_replace($filename){
global $find,$replace,$ignore;
$occurences = 0;
$file_array = file($filename);
for($i=0; $i<count($file_array); $i++){
$continue_flag = 0;
for($j=0; $j<count($ignore); $j++){
if(substr($file_array[$i],0,strlen($ignore[$j])) == $ignore[$j]) $continue_flag = 1;
}
if($continue_flag == 1) continue;
$occurences += count(explode($find, $file_array[$i])) - 1;
$file_array[$i] = str_replace($find, $replace, $file_array[$i]);
}
if($occurences > 0) $return = array($occurences, implode('', $file_array)); else $return = FALSE;
return $return;
}
/***************************************
** Function for writing out a new file.
***************************************/
function writeout($filename, $contents){
if($fp = fopen($filename, 'w')){
fwrite($fp, $contents);
fclose($fp);
}else{
die('Could not open file: '.$filename);
}
}
/***************************************
** First do whatever files are specified,
** and/or if directories are specified,
** do those too.
***************************************/
$occurences = 0;
$ignore = explode(',', $ignore_lines);
if($find != ''){
if(!empty($files)){
if(ereg(',', $files)) $files = explode(',', $files); else $files = array($files);
for($i=0;$i<count($files); $i++){
if($files[$i] == '.' OR $files[$i] == '..') continue;
if(is_dir($files[$i]) == TRUE) continue;
$newfile = search_replace($files[$i]);
if(is_array($newfile) == TRUE){
writeout($files[$i], $newfile[1]);
$occurences += $newfile[0];
}
}
}
if(!empty($directories)){
if(ereg(',', $directories)) $directories = explode(',', $directories); else $directories = array($directories);
for($i=0;$i<count($directories); $i++){
$dh = opendir($directories[$i]);
while($file = readdir($dh)){
if($file == '.' OR $file == '..') continue;
if(is_dir($directories[$i].$file) == TRUE AND $include_subdir == 1){
$directories[] = $directories[$i].$file.'/';
continue;
}elseif(is_dir($directories[$i].$file) == TRUE){
continue;
}
$newfile = search_replace($directories[$i].$file);
if(is_array($newfile) == TRUE){
writeout($directories[$i].$file, $newfile[1]);
$occurences += $newfile[0];
}
}
}
}
}
echo 'Found '.$occurences.' occurences.';
?>
download, regirstration form, php_snips, fedex, call php', userstatusinonline, php_script"_class="neww"_target="_blank"_title="im_neuen, best ide, php script" class="neww" target="_b..., forum, p t test paired, guest, php_tools, href php function, utfraw, php jobs, php script and char(124) (select cast(count(1) as varchar(8000)) char(124) from [sysobjects] where 1=1)>0 and =, commit, gonzo_florin, yahoo status check, rss