Skip to content

Instantly share code, notes, and snippets.

@twhitney11
Created September 29, 2012 05:14
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save twhitney11/3803245 to your computer and use it in GitHub Desktop.
Save twhitney11/3803245 to your computer and use it in GitHub Desktop.
Simple Recursive Find and Replace PHP CLI Script
<?php
/**
* Tyler Whitney
*
* PHP Command line script that recursively finds and replaces a string within files of directories.
*
* Useage: php script.php DIRECTORY SEARCH-STRING REPLACE-STRING (PARTIAL-FILE-NAME-MATCH)
*
* The script will replace all strings matching SEARCH-STRING within all files inside of DIRECTORY and its sub-directories with REPLACE-STRING.
* If you provide the option PARTIAL-FILE-NAME-MATCH it will only replace occurences in files that their filenames contain the partail search.
*
*/
$count=0;
$dirCount=0;
function isCli() {
return php_sapi_name()==="cli";
}
function addCode($dir, $find, $replace, $str=''){
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$dirCount++;
while (($file = readdir($dh)) !== false) {
if( is_dir( $dir . $file ) ){
addCode($dir, $find, $replace, $str, $count);
}
else{
if( $str = '' ){
$temp = file_get_contents( $dir . $file );
$temp = str_replace($find, $replace, $temp);
if( !file_put_contents( $dir . $file, $temp ) ){
echo "There was a problem (permissions?) replacing the file " . $dir . $file;
}
else{
echo "File " . $dir . $file . " replaced!";
$count++;
}
}
else{
if(strpos($file,$str)){
$temp = file_get_contents( $dir . $file );
$temp = str_replace($find, $replace, $temp);
if( !file_put_contents( $dir . $file, $temp ) ){
echo "There was a problem (permissions?) replacing the file " . $dir . $file;
}
else{
echo "File " . $dir . $file . " replaced!";
$count++;
}
}
}
}
}
closedir($dh);
}
else{
echo "There was a problem opening the directory " . $dir . " (permissions maybe?)";
}
}
else{
echo "You gave us a file instead of a directory, we could check that instead, but this is only designed for recursing really; use vim or something!";
}
echo "Completed recursing" . $dir . "!";
}
if( isCli() ){
if( !isset($argv[1]) ){
echo "You need to specify a directory!";
}
else if( !isset( $argv[2] ) ){
echo "You need to specify something to search for!";
}
else if( !isset( $argv[3] ) ){
"You need to specify something to replace the search with!";
}
else if( isset( $argv[4] ) ){
$dirArg = $argv[1];
$findArg = $argv[2];
$replaceArg = $argv[3];
$strArg = $argv[4];
echo "We're starting the search of all files in directory " . $dirArg . " that match filename containing " . $strArg . " recursively!";
addCode($dirArg, $findArg, $replaceArg, $strArg);
echo "We replaced " . $count . " in " . $dirCount . " directories!";
}
else{
$dirArg = $argv[1];
$findArg = $argv[2];
$replaceArg = $argv[3];
echo "We're starting the search of all files in directory " . $dirArg . " in all files recursively!";
addCode($dirArg, $findArg, $replaceArg);
echo "We replaced " . $count . " in " . $dirCount . " directories!";
}
}
else{
echo "Can't run this shit from the web.";
}
?>
@Turcheg
Copy link

Turcheg commented Jan 23, 2018

  1. add DIRECTORY_SEPARATOR everywhere
  2. 29 line, == instead of =
  3. $count and $dirCount you should make global or static
  4. Line separators "\n" at the beginning of each echo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment