RE: [PHP-PEAR] Search/Replace class
| From: | Richard Heyes | Date: | Sun, 08 Oct 2000 00:14:07 +0000 |
| Subject: | RE: [PHP-PEAR] Search/Replace class | ||
| References: | 1 | Groups: | php.pear |
| Request: | Send a blank email to php-pear+get-338@lists.php.net to get a copy of this message | ||
> > Would anyone be willing to commit it to (presumably) the
> File/ dir if I
> > posted some code?
>
> If it's pear-i-fied, sure...
Ok. Here it is. Along with an example script and test file not meant for
committal.
--
Richard Heyes
http://www.heyes-computing.net/scripts/
<?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_search_replace{ // {{{ 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; // }}} // {{{ Constructor /** ** 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 search_replace($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 = ''; } // }}} // {{{ get_num_occurences() /** ** 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 get_num_occurences(){ return $this->occurences; } // }}} // {{{ get_last_error() /** ** 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 get_last_error(){ return $this->last_error; } // }}} // {{{ set_find() /** ** Accessor for setting find variable. ** ** @access public ** @param string $find The string/regex to find. ** ** @author Richard Heyes <richard.heyes@heyes-computing.net> **/ function set_find($find){ $this->find = $find; } // }}} // {{{ set_replace() /** ** 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 set_replace($replace){ $this->replace = $replace; } // }}} // {{{ set_files() /** ** 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 set_files($files){ $this->files = $files; } // }}} // {{{ set_directories() /** ** 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 set_directories($directories){ $this->directories = $directories; } // }}} // {{{ set_include_subdir() /** ** 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 set_include_subdir($include_subdir){ $this->include_subdir = $include_subdir; } // }}} // {{{ set_ignore_lines() /** ** 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 set_ignore_lines($ignore_lines){ $this->ignore_lines = $ignore_lines; } // }}} // {{{ set_search_function() /** ** 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 set_search_function($search_function){ switch($search_function){ case 'normal': $this->search_function = 'search'; return TRUE; break; case 'quick' : $this->search_function = 'quick_search'; return TRUE; break; case 'preg' : $this->search_function = 'preg_search'; return TRUE; break; case 'ereg' : $this->search_function = 'ereg_search'; return TRUE; break; default : $this->last_error = 'Invalid search function specified'; return FALSE; break; } } // }}} // {{{ search() /** ** 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() /** ** 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 quick_search($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() /** ** 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 preg_search($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() /** ** 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 ereg_search($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; } // }}} // {{{ writeout() /** ** 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; } } // }}} // {{{ do_files() /** ** Function called by do_search() 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 do_files($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]; } } } // }}} // {{{ do_directories() /** ** Function called by do_search() 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 do_directories($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]; } } } } // }}} // {{{ do_directories() /** ** 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 do_search(){ if($this->find != ''){ if((is_array($this->files) AND count($this->files) > 0) OR $this->files != '') $this->do_files($this->search_function); if($this->directories != '') $this->do_directories($this->search_function); } } // }}} } ?><?php /*************************************** ** Title........: Search and Replace class ** Filename.....: example.php ** Author.......: Richard Heyes ** Version......: See script ** Notes........: ** Last changed.: 10/09/2000 ** Last change..: ***************************************/ include('search_replace.php'); /*************************************** ** Create the object, set the search ** function and run it. Then change the ** pattern to find something else, and ** re-run the search. ***************************************/ $sr = new file_search_replace('test', 'Replaced!', array('test.txt'), '', 1, array('##')); /*************************************** ** Following function not necessary as ** normal is the default, but here to ** illustrate it. ***************************************/ $sr->set_search_function('normal'); $sr->do_search(); $sr->set_find('another'); $sr->do_search(); /*************************************** ** Some ouput purely for the example. ***************************************/ header('Content-Type: text/plain'); echo 'Number of occurences found: '.$sr->get_num_occurences()."\r\n"; echo 'Error message.............: '.$sr->get_last_error()."\r\n"; ?> NOTE:! Remember that this file must be writeable by the script for it to work... ## test test another
<?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_search_replace{ // {{{ 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; // }}} // {{{ Constructor /** ** 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 search_replace($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 = ''; } // }}} // {{{ get_num_occurences() /** ** 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 get_num_occurences(){ return $this->occurences; } // }}} // {{{ get_last_error() /** ** 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 get_last_error(){ return $this->last_error; } // }}} // {{{ set_find() /** ** Accessor for setting find variable. ** ** @access public ** @param string $find The string/regex to find. ** ** @author Richard Heyes <richard.heyes@heyes-computing.net> **/ function set_find($find){ $this->find = $find; } // }}} // {{{ set_replace() /** ** 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 set_replace($replace){ $this->replace = $replace; } // }}} // {{{ set_files() /** ** 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 set_files($files){ $this->files = $files; } // }}} // {{{ set_directories() /** ** 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 set_directories($directories){ $this->directories = $directories; } // }}} // {{{ set_include_subdir() /** ** 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 set_include_subdir($include_subdir){ $this->include_subdir = $include_subdir; } // }}} // {{{ set_ignore_lines() /** ** 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 set_ignore_lines($ignore_lines){ $this->ignore_lines = $ignore_lines; } // }}} // {{{ set_search_function() /** ** 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 set_search_function($search_function){ switch($search_function){ case 'normal': $this->search_function = 'search'; return TRUE; break; case 'quick' : $this->search_function = 'quick_search'; return TRUE; break; case 'preg' : $this->search_function = 'preg_search'; return TRUE; break; case 'ereg' : $this->search_function = 'ereg_search'; return TRUE; break; default : $this->last_error = 'Invalid search function specified'; return FALSE; break; } } // }}} // {{{ search() /** ** 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() /** ** 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 quick_search($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() /** ** 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 preg_search($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() /** ** 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 ereg_search($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; } // }}} // {{{ writeout() /** ** 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; } } // }}} // {{{ do_files() /** ** Function called by do_search() 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 do_files($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]; } } } // }}} // {{{ do_directories() /** ** Function called by do_search() 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 do_directories($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]; } } } } // }}} // {{{ do_directories() /** ** 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 do_search(){ if($this->find != ''){ if((is_array($this->files) AND count($this->files) > 0) OR $this->files != '') $this->do_files($this->search_function); if($this->directories != '') $this->do_directories($this->search_function); } } // }}} } ?><?php /*************************************** ** Title........: Search and Replace class ** Filename.....: example.php ** Author.......: Richard Heyes ** Version......: See script ** Notes........: ** Last changed.: 10/09/2000 ** Last change..: ***************************************/ include('search_replace.php'); /*************************************** ** Create the object, set the search ** function and run it. Then change the ** pattern to find something else, and ** re-run the search. ***************************************/ $sr = new file_search_replace('test', 'Replaced!', array('test.txt'), '', 1, array('##')); /*************************************** ** Following function not necessary as ** normal is the default, but here to ** illustrate it. ***************************************/ $sr->set_search_function('normal'); $sr->do_search(); $sr->set_find('another'); $sr->do_search(); /*************************************** ** Some ouput purely for the example. ***************************************/ header('Content-Type: text/plain'); echo 'Number of occurences found: '.$sr->get_num_occurences()."\r\n"; echo 'Error message.............: '.$sr->get_last_error()."\r\n"; ?> NOTE:! Remember that this file must be writeable by the script for it to work... ## test test another