Powered by Blogger.

Search & replace in files using php

Searching and replacing content in files is a common task all of us do regularly. Most programmers will implement it using Perl a shell script or through a editor. Perl offers itself as an excellent tool for the required purpose; we PHP programmers are not quite so lucky in that matter. Search/replace is easier from a shell prompt or an editor, but what if you have to do the same programatically in php. File_SearchReplace is a pear package that helps you search/replace in files through a nice object oriented interface.


Installation
Pear installation as usual is simple.
c:/> pear install File_SearchReplace
Doing a simple search & replace
The following is an simple example code that searches the file ‘fruits.txt’ and replaces all occurrences of ‘apples’ with ‘oranges’. The getNumOccurences function returns the total number of replaced strings in the file.
<?php
 
include 'File/SearchReplace.php' ;
 
$files_to_search = array("fruits.txt") ;
$search_string  = "apples";
$replace_string = "oranges";
 
$snr = new File_SearchReplace($search_string,
                              $replace_string,
                              $files_to_search,
                              '', // directorie(s) to search
                              false) ;
 
$snr->doSearch();
 
echo "The number of replaces done : " . $snr->getNumOccurences();
 
?>
The fourth option in the File_SearchReplace specifies a optional directory name to search. If the directory name is empty than the files will be searched in the current directory or in the respective path if a path is also included with the filename. Following is an example if you want to search all files in the directory ‘nature/fruits’.
<?php
 
include 'File/SearchReplace.php' ;
 
$files_to_search = array();
$direc_to_search = array('nature/fruits/');
 
$search_string  = "apples";
$replace_string = "oranges";
 
$snr = new File_SearchReplace($search_string,
                              $replace_string,
                              $files_to_search,
                              $direc_to_search, // directory to search
                              true) ; // 'true' to search subdirectories
 
$snr->doSearch();
 
echo "The number of replaces done : " . $snr->getNumOccurences();
 
?>
All the four starting options of File_SearchReplace are of mixed type; i.e they take a string or an array of strings as their options. For example in the following all occurrences of ‘apples’ will be replaced by ‘oranges’ and that of ‘pears’ by ‘grapes’.
<?php
 
include 'File/SearchReplace.php' ;
 
$files_to_search = array();
$direc_to_search = array('nature/fruits/');
 
$search_string  = array('apples', 'pears');
$replace_string = array('oranges', 'grapes');
 
$snr = new File_SearchReplace($search_string,
                              $replace_string,
                              $files_to_search,
                              $direc_to_search,
                              true) ;
$snr->doSearch();
 
echo "The number of replaces done : " . $snr->getNumOccurences();
 
?>
You don’t have to create a new instance everytime you need a new search, you can set the various parameters through the interface provided by the File_SearchReplace class as shown below.
<?php
 
include 'File/SearchReplace.php' ;
 
$files_to_search = array();
$direc_to_search = array('nature/fruits/');
 
$search_string  = array('apples', 'pears');
$replace_string = array('oranges', 'grapes');
 
$snr = new File_SearchReplace($search_string,
                              $replace_string,
                              $files_to_search,
                              $direc_to_search,
                              true) ;
$snr->doSearch();
 
echo "The number of replaces done : " . $snr->getNumOccurences();
 
/* Start a new search */
 
$snr->setFind( "oranges") ;
$snr->setReplace( "berries") ;
$snr->doSearch();
 
echo "The number of replaces done : " . $snr->getNumOccurences();
 
?>
Regular Expression search
You can also use a regular expression in a search string, but before that we must specify what kind of search is required with the ‘setSearchFunction’ as shown below. The following example replaces all occurrences of ‘color’ or ‘colour’ with the capital ‘COLOR’.
<?php
 
include 'File/SearchReplace.php' ;
 
$files_to_search = array();
$direc_to_search = array('test/graphics/');
 
$search_string  = '/col(o|ou)r/';
$replace_string = 'COLOR';
 
$snr = new File_SearchReplace($search_string,
                              $replace_string,
                              $files_to_search,
                              $direc_to_search,
                              true) ;
 
$snr->setSearchFunction("preg");
$snr->doSearch();
 
echo "The number of replaces done : " . $snr->getNumOccurences();
 
?>
The setSearchFunction takes one of the following four options:
normal – default
quick – use str_replace()
preg – use preg_replace()
ereg – use ereg_replace()
More information on this options can be found here.
In conclusion
The package can be quite useful when you want to replace large quantities of text programatically. As it works on plain strings and regular expressions, its can be quite a handy tool in many occasions.
Source : codediesel.com

    Blogger Comment
    Facebook Comment