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: 575 This week: 1All time: 5,345 This week: 560Up
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: 61
All time rank: 1460219 in United States United States
Week rank: 411 Up48 in United States United States Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php

include 'search_replace.php';

if ( isset(
$_POST['test'] ) && $_POST['test'] && trim( $_POST['path'] ) != '' )
{
   
$recursive = ( !isset( $_POST['recursive'] ) ) ? 0 : $_POST['recursive'];
   
$whole = ( !isset( $_POST['whole'] ) ) ? 0 : $_POST['whole'];
   
$case = ( !isset( $_POST['case'] ) ) ? 0 : $_POST['case'];
   
$regex = ( !isset( $_POST['regex'] ) ) ? 0 : $_POST['regex'];
   
$ext = ( !isset( $_POST['ext'] ) ) ? null : $_POST['ext'];
   
$mime = ( !isset( $_POST['mime'] ) ) ? 0 : $_POST['mime'];

   
$test = new search_replace( $_POST['path'], $_POST['needle'], $_POST['replace'] );

   
$test->set_case( $case );
   
$test->set_whole_word( $whole );
   
$test->set_regex( $regex );
   
$test->set_recursive( $recursive );
   
$test->set_extension( $ext );
   
$test->set_mime( $mime );

   
$result = $test->get_results();

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Text Based File Search &amp; Replace</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
<!--
body
{
    min-width: 800px;
    margin: 0 3%;
    background: #fff;
    color: #000;
    font-family: verdana,arial,sans-serif;
    font-size: 75%;
}
#container
{
    background: #efefef;
    border: 1px solid #ccc;
    -moz-border-radius: 5px;
    border-radius: 5px;
    padding: 0 5px 20px 5px;
    clear: both;
}
#header
{
    margin-top: 0;
    margin-bottom: 20px;
    padding: 10px;
    -moz-border-radius: 6px;
    border-bottom-right-radius: 6px;
    border-bottom-left-radius: 6px;
    box-shadow: 0px 3px 1px #ccc;
    background: #383838;
    /* gecko based browsers */
    background: -moz-linear-gradient(top, #444, #000);
    /* webkit based browsers */
    background: -webkit-gradient(linear, left top, left bottom, from(#444), to(#000));
    color: #fff; /* text colour (black) */
    height: auto; /* gradient uses the full height of the element */
}
 #form_table
{
    width: 100%;
    border-collapse: seperate;
    border-spacing: 8px;
}
#form_table td
{
    -moz-border-radius: 3px;
    border-radius: 3px;
    background: #ddd;
    color: #000;
    padding: 5px;
}
input
{
    padding: 3px;
}
.align_elem
{
    text-align: left;
}
.align_ctrl
{
    text-align: center;
}
-->
</style>
</head>
<body>
    <div id="header">
        <h1 style="font-style:oblique;padding-left:20px;">Text Based File Search &amp; Replace</h1>
    </div>
    <div id="container">
        <form action="demo.php" method="post">
            <input type="hidden" name="test" value="1" />
            <table id="form_table">
                <tr>
                    <th class="align_elem" colspan="2">PHP Search and Replace is a PHP 5 class that can search and replace strings in multiple text based files and subdirectories.<br /><br />
                    </th>
                </tr>
                <tr>
                    <th class="align_elem" colspan="2">Enter Directory Path</th>
                </tr>
                <tr>
                    <td class="align_elem"><input type="text" name="path" /></td>
                    <td class="align_elem" style="width:100%;">Enter absolute or relative path - ie: public_html/scripts - C:\Apache\htdocs\scripts</td>
                </tr>
                <tr>
                    <th class="align_elem" colspan="2">Enter Search String</th>
                </tr>
                <tr>
                    <td class="align_elem"><textarea name="needle" /></textarea></td>
                    <td class="align_elem">Enter string to search for</td>
                </tr>
                <tr>
                    <th class="align_elem" colspan="2">Regular Expression</th>
                </tr>
                <tr>
                    <td class="align_ctrl"><input type="checkbox" name="regex" value="1" />&nbsp;&nbsp;Check to enable</td>
                    <td class="align_elem">Search with regular expression - overides "Match Whole Word"</td>
                </tr>
                <tr>
                    <th class="align_elem" colspan="2">Enter Replacement String</th>
                </tr>
                <tr>
                    <td class="align_elem"><textarea name="replace" /></textarea></td>
                    <td class="align_elem">Enter replacement string</td>
                </tr>
                 <tr>
                    <th class="align_elem" colspan="2">Exclusive File Types</th>
                </tr>
                <tr>
                    <td class="align_elem"><input type="text" name="ext" value="" /></td>
                    <td class="align_elem">Enter exclusive extension(s) to search - separate with comma - ie: php,css,sql</td>
                </tr>
                 <tr>
                    <th class="align_elem" colspan="2">Recursive Search</th>
                </tr>
                <tr>
                    <td class="align_ctrl"><input type="checkbox" name="recursive" value="1" />&nbsp;&nbsp;Check to enable</td>
                    <td class="align_elem">Perform recursive search on subdirectories</td>
                </tr>
                 <tr>
                    <th class="align_elem" colspan="2">Match Whole Word</th>
                </tr>
                <tr>
                    <td class="align_ctrl"><input type="checkbox" name="whole" value="1" />&nbsp;&nbsp;Check to enable</td>
                    <td class="align_elem">Match whole word only</td>
                </tr>
                <tr>
                    <th class="align_elem" colspan="2">Case Sensitive Match</th>
                </tr>
                <tr>
                    <td class="align_ctrl"><input type="checkbox" name="case" value="1" />&nbsp;&nbsp;Check to enable</td>
                    <td class="align_elem">Match by proper case</td>
                </tr>
                 <tr>
                    <th class="align_elem" colspan="2">Overide Text MIME Type</th>
                </tr>
                <tr>
                    <td class="align_ctrl"><input type="checkbox" name="mime" value="1" />&nbsp;&nbsp;Check to enable</td>
                    <td class="align_elem">Search all file types - Enable with caution: this will replace strings in all files including images, zip files, etc.</td>
                </tr>
                <tr>
                    <th colspan="2">&nbsp;</th>
                </tr>
                <tr>
                    <td colspan="2" class="align_ctrl"><input type="submit" value="Replace" style="padding:5px 20px;" />&nbsp;&nbsp;&nbsp;<input type="reset"style="padding:5px 16px;" value="Clear Form" /></td>
                </tr>
                <tr>
                    <th class="align_elem" colspan="2" style="padding:10px;">
                    <?php

                   
if ( isset( $_POST['test'] ) && $_POST['test'] && $_POST['path'] != '' )
                    {
                        echo
"Search string: <span style=\"font-weight:normal\">{$_POST['needle']}</span><br />";
                        echo
"Replacement string: <span style=\"font-weight:normal\">{$_POST['replace']}</span><br />";
                        echo
"<p>Files searched: {$result[0]}<br />";
                        echo
"Files modified: {$result[1]}</p>";

                        echo
"<p style=\"height:100px;overflow:auto;background:#fff;padding:8px;border:1px solid #ccc;\">";

                        if (
$result[1] )
                        {
                           
$count = count( $result[2] );

                            for (
$i = 0; $i < $count; $i++ )
                                echo
$result[2][$i] . ' - ' . $result[3][$i] . ' instance(s)<br />';
                        }

                        echo
'</p>';
                    }
                    else
                    {
                        echo
"<p>Files searched:<br />";
                        echo
"Files modified:</p>";
                    }

                   
?>
</th>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>


Details

App: PHP Search and Replace Version: 1.0.1 Author: MT Jordan <mtjo62@gmail.com> 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+ Restrictions: * 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 Requirements: * PHP 5.2+ (5.4+ recommended) ********************************************************************************* Usage: See demo.php for a working example <?php 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  
 0%
Total:575
This week:1
All time:5,345
This week:560Up
User Ratings User Comments (2)
 All time
Utility:83%StarStarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:83%StarStarStarStarStar
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:66%StarStarStarStar
Rank:517
 
This is an excellent search and replace utility.
3 years ago (Mark)
80%StarStarStarStarStar
full file
9 years ago (hassan ahmadi)
80%StarStarStarStarStar