Source for file SearchReplace.php
Documentation is available at SearchReplace.php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2005, Richard Heyes |
// | All rights reserved. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard@phpguru.org> |
// +-----------------------------------------------------------------------+
// $Id: SearchReplace.php,v 1.12 2005/03/11 20:32:40 techtonik Exp $
// Search and Replace Utility
* Search and Replace Utility
* @author Richard Heyes <richard@phpguru.org>
// {{{ Properties (All private)
* @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 bool $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.
function File_SearchReplace($find, $replace, $files, $directories = '', $include_subdir = TRUE , $ignore_lines = array ())
$this->php5 = (substr(PHP_VERSION , 0 , 1 ) == 5 ) ? TRUE : FALSE;
// {{{ getNumOccurences()
* Accessor to return the number of occurences found.
* @return int Number of occurences found.
* Accessor for retrieving last error.
* @return string The last error that occurred, if any.
* Accessor for setting find variable.
* @param string $find The string/regex to find.
* Accessor for setting replace variable.
* @param string $replace The string/regex to replace the find string/regex with.
* Accessor for setting files variable.
* @param array $files The file(s) to perform this operation on.
* Accessor for setting directories variable.
* @param array $directories The directories to perform this operation on.
* Accessor for setting include_subdir variable.
* @param bool $include_subdir Whether to traverse subdirectories or not.
* Accessor for setting ignore_lines variable.
* @param array $ignore_lines Ignore lines beginning with any of the strings in this array. This
* feature only works with the "normal" search.
// {{{ setSearchFunction()
* Function to determine which search function is used.
* @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.
switch($search_function) {
default : $this->last_error = 'Invalid search function specified';
* Default ("normal") search routine.
* @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.
function search ($filename)
$file_array = file($filename);
} else { // str_replace() doesn't return number of occurences in PHP4
// so we need to count them manually and/or filter strings
// just for the sake of catching occurences
for ($i=0; $i < count($file_array); $i++ ) {
if ($ignore_lines_num > 0 ) {
for ($j=0; $j < $ignore_lines_num; $j++ ) {
foreach ($local_find as $fk => $ff) {
$fr = (isset ($local_replace[$fk])) ? $local_replace[$fk] : "";
$file_array[$i] = str_replace($ff, $fr, $file_array[$i]);
if ($occurences > 0 ) $return = array ($occurences, implode('', $file_array)); else $return = FALSE;
* @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.
function quickSearch ($filename)
// logic is the same as in str_replace function with one exception:
// if <search> is a string and <replacement> is an array - substitution
// is done from the first element of array. str_replace in this case
// usualy fails with notice and returns "ArrayArrayArray..." string
// (this exclusive logic of SearchReplace will not work for php5, though,
// because I haven't decided yet whether it is bug or feature)
foreach ($local_find as $fk => $ff) {
$fr = (isset ($local_replace[$fk])) ? $local_replace[$fk] : "";
if ($occurences > 0 ) $return = array ($occurences, $file); else $return = FALSE;
* @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.
function pregSearch ($filename)
foreach($local_find as $fk => $ff) {
$fr = (isset ($local_replace[$fk])) ? $local_replace[$fk] : "";
if ($occurences > 0 ) $return = array ($occurences, $file); else $return = FALSE;
* @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.
function eregSearch ($filename)
foreach($local_find as $fk => $ff) {
$fr = (isset ($local_replace[$fk])) ? $local_replace[$fk] : "";
if ($occurences > 0 ) $return = array ($occurences, $file); else $return = FALSE;
* Function to writeout the file contents.
* @param string $filename The filename of the file to write.
* @param string $contents The contents to write to the file.
function writeout ($filename, $contents)
if ($fp = @fopen($filename, 'w')) {
$this->last_error = 'Could not open file: '. $filename;
* Function called by doSearch() to go through any files that need searching.
* @param string $ser_func The search function to use.
function doFiles ($ser_func)
if ($this->files[$i] == '.' OR $this->files[$i] == '..') continue;
$newfile = $this->$ser_func($this->files[$i]);
$this->writeout ($this->files[$i], $newfile[1 ]);
* Function called by doSearch() to go through any directories that need searching.
* @param string $ser_func The search function to use.
function doDirectories ($ser_func)
if ($file == '.' OR $file == '..') continue;
$newfile = $this->$ser_func($this->directories[$i]. $file);
$this->writeout ($this->directories[$i]. $file, $newfile[1 ]);
* This starts the search/replace off. Call this to do the search.
* First do whatever files are specified, and/or if directories are specified,
Documentation generated on Mon, 11 Mar 2019 14:05:37 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|