Created
March 11, 2015 13:59
-
-
Save folletto/3884a2f1d1e60f11987e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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