PEAR Manual
Prev |  | Next

# Introduction

Introduction -- HowTo s'n'r in files

## Using

With SearchReplace, you can replace a text in as many as desired files by another.

Example 38-1. Typical usage
    <?php
    include 'File/SearchReplace.php' ;
    $files = array( "test1.txt",
                    "test2.txt",
                    "test3.txt" ) ;
    $ignoreline = array( "#", ":") ;
    $snr = new File_SearchReplace( "Yes", "No", $files, "/mail/", false,
                                $ignoreline) ;
    $snr -> doSearch() ;
    ?>

The example replaces all occurences of `"Yes"` with `"No"` in the given $files and in all files in the directory `"/mail/"`. If a line in a file starts with one of the chars in `$ignoreline`, possible matches will be ignored.

You can do a new search without creating a new instance of the class.

Example 38-2. Do a new search
    ...
    // string to search
    $snr -> setFind( "Er") ;
    // string to find
    $snr -> setReplace( "Sie") ;
    // look in this files
    $snr -> setFiles( $files) ;
    // look in this directories
    $snr -> setDirectories( array( "/neue_briefe/")) ;
    // look in the subdirectories too
    $snr -> setIncludeSubdir( true) ;
    // ignore lines in the files starting with this chars
    $snr -> setIgnoreLines( array( "::", "#")) ;
    // restart search'n'replace
    $snr -> doSearch() ;

## Types of search functions

File_SearchReplace supports different kinds of search functions. The type directly influence the format of the required `$find`-parameter

`normal` - default, the only type supporting the `$IgnoreLines`-parameter
`quick` - use str_replace()
`preg` - use preg_replace()
`ereg` - use ereg_replace()

To set the type, call setSearchFunction() before doSearch().

Prev | Home | Next
File_SearchReplace | Up | File_SearchReplace::File_SearchReplace()