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
-
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().
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
-
mixed $find- the string/regex or array of strings/regexes to find -
mixed $replace- the string/regex or array of strings/regexes to replace $find -
array $files- the file(s) to perform this operation on. -
array $directories- the directories to perform this operation on. -
boolean $include_subdir- if performing on directories, whether to traverse subdirectories. -
array $ignore_lines- ignore lines beginning with any of the strings in this array. This feature only works with the "normal" search.
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
-
string $directories- the directories to perform
Note
This function can not be called statically.
See
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
-
array $files- the file(s) to scan
Note
This function can not be called statically.
See
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
-
mixed $find- the string/regex or arrays of them to find
Note
This function can not be called statically.
See
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
-
boolean $inlude_subdir- Whether to traverse subdirectories or not.
Note
This function can not be called statically.
See
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
-
mixed $replace- the string/regex or arrays of them to replace found string/regex with.
Note
This function can not be called statically.
See
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
-
$search_function- The search function that should be used. Possible values:"normal","quick","preg","ereg"
Note
This function can not be called statically.
See
File_SearachReplace constructor() ,"Types of search functions"