Skip to content

Instantly share code, notes, and snippets.

@mpdroog
Last active January 30, 2021 13:46
Show Gist options
  • Save mpdroog/32eabf0556f0d316d2d4bd38793c075e to your computer and use it in GitHub Desktop.
Save mpdroog/32eabf0556f0d316d2d4bd38793c075e to your computer and use it in GitHub Desktop.
<?php
// Search&Replace all occurrences of a given string
// Got tired of all the harship with sed -i so made this
// small script to get the same result.
$search = "Fn";
$replace = "Shared";
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
foreach ($rii as $file) {
if (in_array($file->getFilename(), [".", "..", "findreplace.php", ".git"])) continue; // Ignored files (in curdir)
if (strpos($file->getPath(), ".git") !== false) continue; // Ignore everything in the .git dir
if (strpos($file->getFilename(), ".php") === false) continue; // Only parse .php files
$content = file_get_contents($file);
if (strpos($content, $search) !== false) {
var_dump($file);
$content = str_replace($search, $replace, $content);
file_put_contents($file, $content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment