PHP Classes

PHP Search And Replace in File: Search and replace strings in text files

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum (4)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 66%Total: 566 All time: 5,371 This week: 154Up
Version License PHP version Categories
search_replace 1.0.1Custom (specified...5.2PHP 5, Files and Folders, Searching
Description Author

This class can search and replace strings in text files.

It can traverse a given directory recursively and find files that match a given MIME type, or file name extension, or a regular expression.

For text files the class can search for a given text and replace it with a given replacement string.

The files found and replaced are returned as an array of path to the files.

Picture of Mike Jordan
  Performance   Level  
Name: Mike Jordan <contact>
Classes: 5 packages by
Country: United States United States
Age: 59
All time rank: 1466214 in United States United States
Week rank: 485 Up56 in United States United States Up
Innovation award
Innovation award
Nominee: 1x

App:       PHP Search and Replace
Version:   1.0.1
Author:    MT Jordan <>
Copyright: 2014
License:   zlib/libpng License


PHP Search and Replace

PHP Search and Replace is a PHP 5 class that can search and replace strings
in multiple text based files and subdirectories.


PHP Search and Replace Features:

    * Search and replace text strings on multiple text based files
    * Restrict search to specific file types - ie: php,sql,txt,html,xml
    * Recursively search through subdirectories
    * Search for whole word only
    * Case-sensitive search
    * Search using regular expression as search string
    * Number of files searched and modified returned as an array for reporting
    * Requires no third party libraries
    * Tested on PHP 5.2.17+


    * You must enter an absolute or relative path for the directory - no URLs
    * Only text based files with a MIME type of text/* can be searched and modified
      unless set_mime() method set to true


    * PHP 5.2+ (5.4+ recommended)



See demo.php for a working example


include 'search_replace.php';

//create search object and enter required vars
$test = new search_replace( 'path/to/dir', 'needle', 'replacement' );

//set options
$test->set_case( true or false ); //true for case-sensitive search - default: false
$test->set_whole_word( true or false ); //true for whole word match - default: false
$test->set_regex( true or false ); //true for search with regular expression - default: false
$test->set_recursive( true or false ); //true to search through subdirectories - default: false
$test->set_extension( 'php,css,xml' ); //comma delimited string of exclusive extensions - default: null
$test->set_mime( true or false ); //true to search all file types - default: false

//perform search/replace and return results
$result = $test->get_results();

echo $result[0]; //number of files searched
echo $result[1]; //number of files modified
echo $result[2]; //modified file path
echo $result[3]; //number of replacement occurrences

  Files folder image Files  
File Role Description
Accessible without login HTML file demo.html Output Demonstration Search and Replace Form
Accessible without login Plain text file demo.php Example Demonstration Search and Replace Form
Accessible without login Plain text file license.txt Lic. zlib/libpng License
Accessible without login Plain text file readme.txt Doc. Readme file
Plain text file search_replace.php Class Search and Replace Class

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:5,371
This week:154Up
User Ratings User Comments (2)
 All time
This is an excellent search and replace utility.
1 year ago (Mark)
full file
7 years ago (hassan ahmadi)
For more information send a message to info at phpclasses dot org.