PHP Find/Replace Script
#1
When sifting through old code trying to find different names of things, I've found it extremely useful and time-saving to run a PHP script from the command line that searches through all the files in a directory on my computer for a search term. On this particular project, I have it set to sift through ActionScript files only, but you can set it to anything. Here it is, if anyone's interested:

PHP Code:
<?php

$base_path 
__DIR__ "/fla";

if (!isset(
$argv[2])) {
    
$argv[2] = false;
}

$files getDirContents($base_path);
search_files($files);
die();

function 
getDirContents($dir, &$results = array()){
    
$files scandir($dir);

    foreach(
$files as $key => $value){
        
$path realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!
is_dir($path)) {
            
$results[] = $path;
        } else if(
$value != "." && $value != "..") {
            
getDirContents($path$results);
            
$results[] = $path;
        }
    }

    return 
$results;
}

function 
search_files($files)
{
    global 
$argv;

    
$continue false;
    
$filesChanged 0;
    foreach (
$files as $file) {
        
$file_path $file;
        if (
substr($file, -33) == ".as") {
            
$find $argv[1];
            
$strict $argv[2] == 'true' true false;
            
$file file_get_contents($file_path);
            if (
$strict === false && strpos($file$find) !== false && !isset($argv[3])) {
                echo 
"$file_path\n";
            } elseif (
$strict === true && preg_match("/\b$find\b/"$file) && !isset($argv[3])) {
                echo 
"$file_path\n";
            } elseif (isset(
$argv[3])) {
                if (
$continue === false) {
                    echo 
"\nReplace \"$find\" with \"$argv[3]\"?\nWARNING: THIS CANNOT BE UNDONE.\n\nContinue? (yes/no): ";
                    
$reply trim(strtolower(fgets(fopen("php://stdin","r"))));
                    if (
$reply == 'true' || $reply == 't' || $reply == 'yes' || $reply == 'y') {
                        
$continue true;
                        echo 
"\n";
                    } else {
                        break;
                    }
                }
                if (
$strict === false && strpos($file$find) !== false) {
                    
file_put_contents($file_pathstr_replace($find$argv[3], $file));
                    echo 
"$file_path\n";
                    
$filesChanged++;
                } elseif (
$strict === true && preg_match("/\b$find\b/"$file)) {
                    
file_put_contents($file_pathpreg_replace("/\b$find\b/"$argv[3], $file));
                    echo 
"$file_path\n";
                    
$filesChanged++;
                } else {
                    continue;
                }
            }
        }
    }
    
$continue false;
    if (
$filesChanged 0) {
        echo 
"\nTotal files changed: $filesChanged\n\n";
    } else {
        if (!isset(
$argv[3])) {
            echo 
"\n";
        } else {
            echo 
"\nNo files to be changed.\n\n";
        }
    }


The arguments are:
[0] = File
[1] = Find
[2] = Strict? (yes/true uses preg_match to match exact words, no/false uses strpos for a character match)
[3] = Replace

A sample run on the command line for me would be:

Code:
php /Users/ben/Projects/SuperSecretThings/search.php "Example" true

That would give me a strict, case-sensitive result for the complete word "Example" as opposed to also getting a result for "ExampleData".

Anyway, yep. I hope y'all learned something and/or have a use for the code in your own projects.


I post about the latest site updates on the Dev Log. If you have suggestions, feel free to post them here.
@Eternal and I pay for this site out of our own savings. Please consider donating to help keep Jiggmin's Village running.
The Following 3 Users Say Thank You to bls1999 For This Useful Post:
  • Ashley766, xGuest, Zelante
Reply
#2
interesting, even though I dont know how to code at all and that xd
[Image: boomboomforalltid.gif?width=400&height=119]
The Following 1 User Says Thank You to xGuest For This Useful Post:
  • bls1999
Reply
#3
(8th December 2018, 5:26 AM)xGuest Wrote: interesting, even though I dont know how to code at all and that xd

Haha, I'm actually learning PHP rn, so I understand a little bitTongue
The Following 1 User Says Thank You to Ashley766 For This Useful Post:
  • bls1999
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)