Skip to content

Instantly share code, notes, and snippets.

@folletto
Created March 11, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save folletto/3884a2f1d1e60f11987e to your computer and use it in GitHub Desktop.
Save folletto/3884a2f1d1e60f11987e to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
function getAllDirectories( $base_dir, $list_hidden = false ) {
$out = array();
foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_dir ) ) as $path ) {
// ****** Skip non-relevant files
$filename = basename( $path );
if ($list_hidden == false && substr( $filename, 0, 1 ) == '.' ) continue;
//if ( $filename == '.' || $filename == '..') continue;
$out []= $path;
}
return $out;
}
function checkAllFilesInArrayForRegexMatch( $array, $regex ) {
$matches = array();
foreach ( $array as $path ) {
$matches = array_merge( $matches, checkFileForRegexMatch( $path, $regex ) );
}
return $matches;
}
function checkFileForRegexMatch( $path, $regex ) {
$matches = array();
$handle = fopen( $path, "r" );
if ($handle) {
while ( ( $line = fgets( $handle ) ) !== false ) {
$line_matches = array();
preg_match( $regex, $line, $line_matches );
if ( count( $line_matches ) > 0 ) {
$matches []= $line;
}
}
fclose($handle);
} else {
echo "Error reading '$path'.";
}
return $matches;
}
// ****** RUN
$time_start = microtime();
$alldirs = getAlldirectories( '/home/wpcom/public_html/wp-content/themes/a8c' );
$matches = checkAllFilesInArrayForRegexMatch( $alldirs, '/fallback_cb/' );
print_r( $matches );
echo "\nTotal execution time: " . ( microtime() - $time_start ) . " seconds";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment