Skip to content

Instantly share code, notes, and snippets.

@twhitney11
Last active December 3, 2024 06:02
Show Gist options
  • 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.";
}
?>
@s22-tech
Copy link

s22-tech commented Dec 3, 2024

This code has been updated for PHP 8+. You can find it here

@twhitney11
Copy link
Author

This code has been updated for PHP 8+. You can find it here

Wow, I didn't even remember this, 12 years ago. I can't remember what I needed it for. I think it was when I first started my position at a new place and was migrating from one document management system to another and needed to bulk rename files in directories recursively. Could come in handy, I'll check the PHP 8+ version out. Thanks!

@s22-tech
Copy link

s22-tech commented Dec 3, 2024 via email

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