1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
<?php
//
// Search and Replace Utility
//
//
// +----------------------------------------------------------------------+
// | Author: Richard Heyes <richard.heyes@heyes-computing.net |
// | Version: 1.00 |
// | Updated: 08/10/2000 |
// | |
// | See http://www.heyes-computing.net/scripts/ for full tar/zip |
// | including example file. |
// +----------------------------------------------------------------------+
class File_SearchReplace {
// {{{ Properties (All private)
var $find;
var $replace;
var $files;
var $directories;
var $include_subdir;
var $ignore_lines;
var $ignore_sep;
var $occurences;
var $search_function;
var $last_error;
// }}}
/**
* Sets up the object
*
* @access public
* @param string $find The string/regex to find.
* @param string $replace The string/regex to replace $find with.
* @param array $files The file(s) to perform this operation on.
* @param array $directories (optional) The directories to perform this operation on.
* @param int $include_subdir If performing on directories, whether to traverse subdirectories.
* @param array $ignore_lines Ignore lines beginning with any of the strings in this array. This
* feature only works with the "normal" search.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function File_SearchReplace($find, $replace, $files, $directories = '', $include_subdir = 1, $ignore_lines = array()){
$this->find = $find;
$this->replace = $replace;
$this->files = $files;
$this->directories = $directories;
$this->include_subdir = $include_subdir;
$this->ignore_lines = $ignore_lines;
$this->occurences = 0;
$this->search_function = 'search';
$this->last_error = '';
}
/**
* Accessor to return the number of occurences found.
*
* @access public
* @return int Number of occurences found.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function getNumOccurences(){
return $this->occurences;
}
/**
* Accessor for retrieving last error.
*
* @access public
* @return string The last error that occurred, if any.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function getLastError(){
return $this->last_error;
}
/**
* Accessor for setting find variable.
*
* @access public
* @param string $find The string/regex to find.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function setFind($find){
$this->find = $find;
}
/**
* Accessor for setting replace variable.
*
* @access public
* @param string $replace The string/regex to replace the find string/regex with.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function setReplace($replace){
$this->replace = $replace;
}
/**
* Accessor for setting files variable.
*
* @access public
* @param array $files The file(s) to perform this operation on.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function setFiles($files){
$this->files = $files;
}
/**
* Accessor for setting directories variable.
*
* @access public
* @param array $directories The directories to perform this operation on.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function setDirectories($directories){
$this->directories = $directories;
}
/**
* Accessor for setting include_subdir variable.
*
* @access public
* @param int $include_subdir Whether to traverse subdirectories or not.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function setIncludeSubdir($include_subdir){
$this->include_subdir = $include_subdir;
}
/**
* Accessor for setting ignore_lines variable.
*
* @access public
* @param array $ignore_lines Ignore lines beginning with any of the strings in this array. This
* feature only works with the "normal" search.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function setIgnoreLines($ignore_lines){
$this->ignore_lines = $ignore_lines;
}
/**
* Function to determine which search function is used.
*
* @access public
* @param string The search function that should be used. Can be any one of:
* normal - Default search. Goes line by line. Ignore lines feature only works with this type.
* quick - Uses str_replace for straight replacement throughout file. Quickest of the lot.
* preg - Uses preg_replace(), so any regex valid with this function is valid here.
* ereg - Uses ereg_replace(), so any regex valid with this function is valid here.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function setSearchFunction($search_function){
switch($search_function){
case 'normal': $this->search_function = 'search';
return TRUE;
break;
case 'quick' : $this->search_function = 'quickSearch';
return TRUE;
break;
case 'preg' : $this->search_function = 'pregSearch';
return TRUE;
break;
case 'ereg' : $this->search_function = 'eregSearch';
return TRUE;
break;
default : $this->last_error = 'Invalid search function specified';
return FALSE;
break;
}
}
/**
* Default ("normal") search routine.
*
* @access private
* @param string $filename The filename to search and replace upon.
* @return array Will return an array containing the new file contents and the number of occurences.
* Will return FALSE if there are no occurences.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function search($filename){
$occurences = 0;
$file_array = file($filename);
for($i=0; $i<count($file_array); $i++){
if(count($this->ignore_lines) > 0){
for($j=0; $j<count($this->ignore_lines); $j++){
if(substr($file_array[$i],0,strlen($this->ignore_lines[$j])) == $this->ignore_lines[$j]) continue 2;
}
}
$occurences += count(explode($this->find, $file_array[$i])) - 1;
$file_array[$i] = str_replace($this->find, $this->replace, $file_array[$i]);
}
if($occurences > 0) $return = array($occurences, implode('', $file_array)); else $return = FALSE;
return $return;
}
/**
* Quick search routine.
*
* @access private
* @param string $filename The filename to search and replace upon.
* @return array Will return an array containing the new file contents and the number of occurences.
* Will return FALSE if there are no occurences.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function quickSearch($filename){
clearstatcache();
$file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
$occurences = count(explode($this->find, $file)) - 1;
$file = str_replace($this->find, $this->replace, $file);
if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return;
}
/**
* Preg search routine.
*
* @access private
* @param string $filename The filename to search and replace upon.
* @return array Will return an array containing the new file contents and the number of occurences.
* Will return FALSE if there are no occurences.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function pregSearch($filename){
clearstatcache();
$file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
$occurences = count($matches = preg_split($this->find, $file)) - 1;
$file = preg_replace($this->find, $this->replace, $file);
if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return;
}
/**
* Ereg search routine.
*
* @access private
* @param string $filename The filename to search and replace upon.
* @return array Will return an array containing the new file contents and the number of occurences.
* Will return FALSE if there are no occurences.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function eregSearch($filename){
clearstatcache();
$file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
$occurences = count($matches = split($this->find, $file)) -1;
$file = ereg_replace($this->find, $this->replace, $file);
if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return;
}
/**
* Function to writeout the file contents.
*
* @access private
* @param string $filename The filename of the file to write.
* @param string $contents The contents to write to the file.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function writeout($filename, $contents){
if($fp = @fopen($filename, 'w')){
flock($fp,2);
fwrite($fp, $contents);
flock($fp,3);
fclose($fp);
}else{
$this->last_error = 'Could not open file: '.$filename;
}
}
/**
* Function called by doSearch() to go through any files that need searching.
*
* @access private
* @param string $ser_func The search function to use.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function doFiles($ser_func){
if(!is_array($this->files)) $this->files = explode(',', $this->files);
for($i=0; $i<count($this->files); $i++){
if($this->files[$i] == '.' OR $this->files[$i] == '..') continue;
if(is_dir($this->files[$i]) == TRUE) continue;
$newfile = $this->$ser_func($this->files[$i]);
if(is_array($newfile) == TRUE){
$this->writeout($this->files[$i], $newfile[1]);
$this->occurences += $newfile[0];
}
}
}
/**
* Function called by doSearch() to go through any directories that need searching.
*
* @access private
* @param string $ser_func The search function to use.
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function doDirectories($ser_func){
if(!is_array($this->directories)) $this->directories = explode(',', $this->directories);
for($i=0; $i<count($this->directories); $i++){
$dh = opendir($this->directories[$i]);
while($file = readdir($dh)){
if($file == '.' OR $file == '..') continue;
if(is_dir($this->directories[$i].$file) == TRUE){
if($this->include_subdir == 1){
$this->directories[] = $this->directories[$i].$file.'/';
continue;
}else{
continue;
}
}
$newfile = $this->$ser_func($this->directories[$i].$file);
if(is_array($newfile) == TRUE){
$this->writeout($this->directories[$i].$file, $newfile[1]);
$this->occurences += $newfile[0];
}
}
}
}
/**
* This starts the search/replace off. Call this to do the search.
* First do whatever files are specified, and/or if directories are specified,
* do those too.
*
* @access public
*
* @author Richard Heyes <richard.heyes@heyes-computing.net>
*/
function doSearch(){
if($this->find != ''){
if((is_array($this->files) AND count($this->files) > 0) OR $this->files != '') $this->doFiles($this->search_function);
if($this->directories != '') $this->doDirectories($this->search_function);
}
}
}
?>
|