PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » File System » File_SearchReplace » Manual

Provides an API for replacing text in files

Introduction

Introduction – HowTo s'n'r in files

Using

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

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.

Do a new search

<?php
...
// 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

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

File_SearchReplace::File_SearchReplace()

File_SearchReplace::File_SearchReplace() – constructor

Synopsis

require_once 'File/SearchReplace.php';

void File_SearchReplace::File_SearchReplace ( mixed $find , mixed $replace , string $files , mixed $directories = '' , boolean $include_subdir = true , array $ignore_lines = array() )

Description

Constructor

Parameter

Note

This function can be called statically.

File_SearchReplace::getNumOccurences()

File_SearchReplace::getNumOccurences() – return number of replaces

Synopsis

require_once 'File/SearchReplace.php';

integer File_SearchReplace::getNumOccurences ( )

Description

Returns the number of replaced strings after a File_SearchReplace::doSearch() call.

Return value

integer - the number of occurences

Note

This function can not be called statically.

File_SearchReplace::doSearch()

File_SearchReplace::doSearch() – execute search&replace

Synopsis

require_once 'File/SearchReplace.php';

void File_SearchReplace::doSearch ( )

Description

Starts searching for the patterns and replaces them, if found.

Note

This function can not be called statically.

File_SearchReplace::setDirectories()

File_SearchReplace::setDirectories() – set file location

Synopsis

require_once 'File/SearchReplace.php';

void File_SearchReplace::setDirectories ( array $directories )

Description

Set the directories where the files to scan reside.

Parameter

Note

This function can not be called statically.

See

File_SearachReplace constructor()

File_SearchReplace::setFiles()

File_SearchReplace::setFiles() – set the files to scan

Synopsis

require_once 'File/SearchReplace.php';

void File_SearchReplace::setFiles ( array $files )

Description

Set the files to scan.

Parameter

Note

This function can not be called statically.

See

File_SearachReplace constructor()

File_SearchReplace::setFind()

File_SearchReplace::setFind() – set pattern to find

Synopsis

require_once 'File/SearchReplace.php';

void File_SearchReplace::setFind ( mixed $find )

Description

Set the pattern for the string to find.

Parameter

Note

This function can not be called statically.

See

File_SearchReplace constructor()

File_SearchReplace::setIncludeSubdir()

File_SearchReplace::setIncludeSubdir() – allow scan in sub directories

Synopsis

require_once 'File/SearchReplace.php';

void File_SearchReplace::setIncludeSubdir ( integer $include_subdir )

Description

Enable or disable look up in sub directories for files to scan.

Parameter

Note

This function can not be called statically.

See

File_SearachReplace constructor()

File_SearchReplace::setReplace()

File_SearchReplace::setReplace() – set replace string

Synopsis

require_once 'File/SearchReplace.php';

void File_SearchReplace::setReplace ( mixed $replace )

Description

Set the replacment string.

Parameter

Note

This function can not be called statically.

See

File_SearchReplace constructor()

File_SearchReplace::setSearchFunction()

File_SearchReplace::setSearchFunction() – set search function to use

Synopsis

require_once 'File/SearchReplace.php';

void File_SearchReplace::setSearchFunction ( array $search_function )

Description

Function to determine which search function is used.

Parameter

Note

This function can not be called statically.

See

File_SearachReplace constructor() ,"Types of search functions"