8th December 2018, 5:22 AM
(This post was last modified: 8th December 2018, 5:24 AM by bls1999. Edited 1 time in total.)
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:
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:
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.
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, -3, 3) == ".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_path, str_replace($find, $argv[3], $file));
echo "$file_path\n";
$filesChanged++;
} elseif ($strict === true && preg_match("/\b$find\b/", $file)) {
file_put_contents($file_path, preg_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.