Search and Replace class

Contents

  • Introduction
  • Installation
  • Main Usage
  • Function Reference
  • License

Introduction

This class was originally a script that performed a simple search/replace on files and/or directories. I've now converted it to a class with a few extra features so it should be a little more useful and easier to use. Should also be easier to incorporate into to other scripts.

Installation

Simply copy the class file to your usual "includes" directory and include it the script you wish to use it in.

Main Usage

To use it (after include()ing it), do this:

        $sr = new search_replace('find this', 'replacement', 'file.txt');
        $sr->do_search();
        

The full arguments you can pass when creating the object are described below. There are of course other functions to use, which are also described below.

Function Reference

void search_replace(

        
        string find pattern,
        string Replace pattern,
        mixed  File(s),
        [mixed Directories,
        int    Include Subdirectories,
        array  Ignore Lines])
        
        

Being the constructor you shouldn't call this directly. The arguments are quite self explanatory, the last three being optional.

When using regular expression (see set_search_function()) searching, the find argument is the entire regex, so when using preg it should include the two delimiters and any modifiers you wish to use.

Files should be either a comma seperated string or array of files you want searched. If you specify the directories argument the files argument can be left as a blank string. The directories argument can also be a comma seperated string or an array.

The include subdirectories argument is optional, and should be either 1 (one) or 0 (zero) depending on whether you want subdirectories to be included. This argument is only relevant if you're using the directories argument.

The ignore_lines argument is useful if you wish lines beginning with a certain string to be ignored, but is only supported by one search type (see below). This should be an array.

bool set_search_function(string search type)

This sets the type of search function you wish to use. The argument should be one of the below (lowercase):

normal
The default search function and the only one that supports the ignore_lines feature. This function will go through each line and do the search/replace using str_replace(). Because it splits the file on a newline (\n or ASCII 10), multiline search patterns are not supported.

quick
Basically an enhancement of the default search function. Still uses str_replace(), but does not search line by line, hence this is the quickest search feature. Does not support the ignore_lines feature.

preg
Search and replace using preg_replace. The find pattern should be a valid preg_replace() search pattern, including the delimiters and any modifiers. Does not support the ignore_lines feature.

ereg
Search and replace using ereg_replace. The find pattern should be a valid ereg_replace() search pattern. Does not support the ignore_lines feature.

void do_search(void)

This starts the search. Obviously files will need to be writeable by the script.

int get_num_occurences(void)

This returns the number of times the search pattern was found. If multiple files are searched, this will be cumulative.

string get_last_error(void)

This returns the last error if there is one.

void set_find(string find pattern)

Accessor to set the find pattern.

void set_replace(string replace string)

Accessor to set the replace string.

void set_files(mixed file(s))

Accessor to set the files to be parsed. Argument should be either a comma seperated string or an array.

void set_directories(mixed directories)

Accessor to set the directories to be parsed. Argument should be either a comma seperated string or an array.

void set_include_subdir(int include subdirs)

Accessor to set whether sub-directories are parsed or not. Only used when directories are to be parsed. Should be either 1 (one) or 0 (zero).

void set_ignore_lines(array lines to ignore)

Accessor to set whether sub-directories are parsed or not. Only used when directories are to be parsed. Should be either 1 (one) or 0 (zero). Lines beginning with the strings provided will not be searched.

Bugs

Not bloody likely.

History

10/09/2000 - Version 1.0
Converted plain script to a class, added extra functionality and re-released.

11/04/2000 - Version 0.9
Released as plain script.

License

Postcard-ware. If you use this utility then I'd appreciate a postcard from ya. :)
Otherwise, it's free, be grateful, don't whinge, and don't sue me if it screws your system.

Valid HTML 4.0! Valid CSS 1.0